简体   繁体   English

Powershell plink telnet 命令输出到 var

[英]Powershell plink telnet command output into var

I'm trying to write a script (based on some internet examples) for some telnet/ssh command execution, using powershell and plink.我正在尝试使用 powershell 和 plink 为某些 telnet/ssh 命令执行编写脚本(基于一些互联网示例)。 But the main feature, which I'd like to implement - is to catch this command's output into the variable or a text file.但是我想实现的主要功能是将此命令的输出捕获到变量或文本文件中。 How can I intercept the output of the specific command?如何拦截特定命令的输出?

For example: when I send "get-status" command, it returns "Status is 01 02 03".例如:当我发送“get-status”命令时,它返回“Status is 01 02 03”。 Can I assign this string to the var or text file?我可以将此字符串分配给 var 或文本文件吗? Maybe, only "01 02 03" without "Status" text?也许,只有“01 02 03”没有“状态”文本?

$ps = New-Object -TypeName System.Diagnostics.Process
$ps.StartInfo.UseShellExecute = $false
$ps.StartInfo.RedirectStandardInput = $true
$ps.StartInfo.FileName = "plink"
$ps.StartInfo.Arguments = "-telnet XXX.XXX.XXX.XXX"

[void]$ps.Start() 

$PlinkStreamWriter = $ps.StandardInput

Start-Sleep -m 500
$PlinkStreamWriter.Write("login`r")
Start-Sleep -m 500
$PlinkStreamWriter.Write("password`r")
Start-Sleep -m 500
$PlinkStreamWriter.Write("get-status")
Start-Sleep -m 500

Write-Host "Status string: " .....

$PlinkStreamWriter.Write("exit`r")
$PlinkStreamWriter.Close();
if (!$ps.HasExited) { $ps.Kill() }

If I understand you correctly, when you send 如果我正确理解您的意见,那么在您发送

$PlinkStreamWriter.Write("get-status")

You receive 你收到

Status is 01 02 03

If this is consistent and you want to assign this value to a variable, just define the variable at the beginning of your command: 如果这是一致的,并且您想将此值分配给变量,则只需在命令开头定义变量:

$result =  $PlinkStreamWriter.Write("get-status")

Now if you want to remove "Status is" from the value stored in $result: 现在,如果要从$ result中存储的值中删除“状态为”:

$result = $result -replace "Status is ",""

Now $result contains only "01 02 03" 现在,$ result仅包含"01 02 03"

You can now update your write-host command: 现在,您可以更新您的write-host命令:

Write-Host "Status string: $result"

A simple redirect (>) is all that is needed.只需要一个简单的重定向 (>)。 I verified this works for me in Powershell v5.1.22000.282.我在 Powershell v5.1.22000.282 中验证了这对我有用。 Here is my approach for anyone that needs it.这是我对任何需要它的人的方法。

$filepath = "$PSScriptRoot" $filepath = "$PSScriptRoot"
$PUTTY="$Env:ProgramFiles\\PuTTY\\plink.exe" $PUTTY="$Env:ProgramFiles\\PuTTY\\plink.exe"

@" @"
vncserver -geometry 1920x1080 vncserver -geometry 1920x1080
exit出口
"@| & $PUTTY -ssh $IP -l $USER -pw $PW > "$filepath/out.txt" "@| & $PUTTY -ssh $IP -l $USER -pw $PW > "$filepath/out.txt"

This may be of use, you can redirect the outputs from error messages and warning messages into standard out. 这可能有用,您可以将错误消息和警告消息的输出重定向到标准输出中。 Example: 例:

Invoke-Expression "cmd /c 'C:\\Program Files (x86)\\PuTTY\\plink.exe'$plinkcmd 4>&1 3>&1 2>&1" 调用表达式“ cmd / c'C:\\ Program Files(x86)\\ PuTTY \\ plink.exe'$ plinkcmd 4>&1 3>&1 2>&1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM