powershell - 在 PowerShell 中使用 Plink 创建进程并向其发送命令

标签 powershell process putty interactive plink

我需要能够在远程设备上创建进程并使用 PowerShell 脚本向其发送命令。我需要这样做,因为我在使用命令时会收到提示。我一直在尝试通过使用谷歌搜索和以下链接来弄清楚如何做到这一点:http://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter7.html#plink .从该链接,我意识到在 Plink 中使用带有命令列表的文件将无法工作,原因如下(从该链接复制并粘贴):

Any non-interactive command you could usefully run on the server command line, you can run in a batch file using Plink in this way.

上面写着“非交互式命令”,这就是我正在使用的。无论如何,我也尝试过使用带有命令列表的文件,但它并没有解决我的问题,因为我基本上需要给出一个命令,等待,然后在它提示我时给出另一个命令。这是我在 PuTTY FAQ 中找到的基本上我想尝试的是:

Probably your best bet is to use Plink, the command-line connection tool. If you can start Plink as a second Windows process, and arrange for your primary process to be able to send data to the Plink process, and receive data from it, through pipes, then you should be able to make SSH connections from your program. This is what CVS for Windows does, for example.

编辑: 根据 user2460798 的回答,我尝试了以下解决方案。我希望使用 PuTTY 并能够以这种方式向它发送命令。我现在的问题是我不知道如何向打开的 PuTTY session 发送命令。所以基本上这段代码打开了一个 PuTTY session 并尝试向它写入“ls”命令,但没有任何反应。我不知道“ls”文本的去向。

$procInfo = New-Object Diagnostics.ProcessStartInfo
$procInfo.RedirectStandardOutput=$true
$procInfo.RedirectStandardInput=$true
$procInfo.RedirectStandardError=$true
$procInfo.FileName="putty.exe"
$procInfo.Arguments="-ssh -pw <password> <username>@<device>"
$procInfo.UseShellExecute=$false
$p=[diagnostics.process]::start($procInfo)
sleep -Milliseconds 3000
$p.StandardInput.WriteLine("ls")

最佳答案

不确定是否理解您的问题,但这是我使用 plink 的方式

function plink
{
  [CmdletBinding()]
  PARAM
  (
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $remoteHost,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $login,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $passwd,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $command)

  & c:\Tools\plink.exe -ssh $remoteHost -l $login -pw $passwd $command
  return
}

$remoteHost = "192.168.1.1"
$login = "TheUserWhoCan"

$command1 = "/opt/compiere/apache-tomcat-6.0.32/bin/shutdown.sh "
$command2 = "cd /opt/compiere/apache-tomcat-6.0.32/bin && ./startWS.sh"
$command3 = "ps -edalf | grep java"

$passwd = Read-Host "Enter Password for $login"



write-host "Stopping tomcat" -ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command1 -passwd $passwd
Start-Sleep 10
write-host "Starting tomcat" -ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command2 -passwd $passwd

write-host "Looking for java processes"-ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command3 -passwd $passwd

关于powershell - 在 PowerShell 中使用 Plink 创建进程并向其发送命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180201/

相关文章:

powershell - 在WMI中,是否可以使用联接(或类似方式)获取给定服务器名称和部署位置的站点的IisWebServer对象

powershell - Powershell脚本支持-verbose

powershell - 对 Active Directory 中的数据进行分组

c# - 在 C# 中使用 pscp 通过 post 请求在 Windows 和 Linux 之间传输文件

sql-server - 将比较对象结果添加到差异数据集

c# - 从 Asp.Net Web 应用程序使用 sysinternals PSExec 执行脚本

c - 如何在 Linux 上同步终止进程?

C# 查询进程所有者 (Windows 7 x64)

putty - 使用 PuTTY 从 STM32 打印

linux - 如何在 Putty 上通过 putty 远程连接使用 gedit