在使用PowerShell的Plink执行的注释中使用管道

时间:2017-05-23 12:34:43

标签: shell powershell ssh plink

在PowerShell中,我使用Plink来连接到NetScaler设备。

一切正常,但运行脚本需要很长时间,因为从远程设备下载到本地计算机的数据量很大,我正在运行PowerShell。

这是脚本:

$log = C:\plink.exe -batch -l User -pw PWD 10.1.1.1 "shell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz""

为了提高性能,我需要减少从设备传输的数据量 我只需要域\w+\的第一部分,所以我尝试添加| cut –d . –f 1,但我得到了这个:

cut : The term 'cut' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:144
+ ... hell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f ...
+                                                               ~~~
+ CategoryInfo          : ObjectNotFound: (cut:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException 

我尝试了不同的方法来转义|并将命令放入变量或文件中并使用–m开关。

一切都有相同的结果。

当我使用PuTTY ssh到设备时,cut命令工作正常。

PowerShell,Plink或两者都有这个问题吗?我可以在没有管道的情况下使用cut命令吗?或者还有另一种方法可以返回\w+\部分吗?

以下是使cut工作的各种尝试的输出(不允许从工作中上传屏幕截图)

PS C:\windows\system32> C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS shell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1"
 Done
                                                      ^^^
ERROR: No such command
C:\NetScalerBackup\plink.exe : ERROR: No such command
At line:1 char:1
+ C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS shell  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (ERROR: No such command:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PS C:\windows\system32> C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS "shell `"zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1`""
Done
                                                        ^^^
ERROR: No such command
C:\NetScalerBackup\plink.exe : ERROR: No such command
At line:1 char:1
+ C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS "shell ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (ERROR: No such command:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PS C:\windows\system32> C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS shell zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1
cut : The term 'cut' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:122
+ ... shell zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f ...
+                                                               ~~~
+ CategoryInfo          : ObjectNotFound: (cut:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

更新 @马丁: 当我尝试

"shell `"zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1`"`nexit`n" | C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS

我明白了:

shell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1"
exit


Done
> shell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1"
grep: unknown directories method
ERROR: 
> exit
Bye!

如果我拿出后面的行情(“”)

"shell zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f1`nexit`n" | C:\NetScalerBackup\plink.exe -batch -l $un -pw $pwd $PrimaryNS

这是回复:

Done
> shell zgrep -oE '\w+\.aramco\.com' /var/log/ns.log.0.gz | cut -d . -f1
                                                            ^^^
ERROR: No such command
> exit
Bye!

1 个答案:

答案 0 :(得分:0)

Plink不需要外部引号,只是复杂的解析。这应该有效:

$log = C:\plink.exe -batch -l User -pw PWD 10.1.1.1 shell "zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f"

如果你想保留外部引号,你可以逃避内部引号:

$log = C:\plink.exe -batch -l User -pw PWD 10.1.1.1 "shell `"zgrep -oE '\w+\.some\.com' /var/log/ns.log.0.gz | cut -d . -f`""