简体   繁体   English

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

[英]Create a process using Plink in PowerShell and sending commands to it

I need to be able to create a process on a remote device and send come commands to it using a PowerShell script. 我需要能够在远程设备上创建进程并使用PowerShell脚本向其发送命令。 I need to do it this way because I get prompts when I use a command. 我需要这样做,因为在使用命令时会得到提示。 I have been trying to figure out how to do this by using Google searches and the following link: http://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter7.html#plink . 我一直在尝试通过使用Google搜索和以下链接来解决该问题: http : //tartarus.org/~simon/putty-snapshots/htmldoc/Chapter7.html#plink From that link, I realized using a file with a list of commands in Plink won't work for the following reason (copied and pasted from that link): 从该链接中,我意识到,由于以下原因(从该链接复制和粘贴),无法在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. 您可以在服务器命令行上有用地运行的任何非交互式命令,都可以使用Plink以这种方式在批处理文件中运行。

It says "non-interactive command", which is what I'm using. 它说“非交互式命令”,这就是我正在使用的命令。 I have also tried using a file with a list of commands anyway, but it didn't solve my problem because I basically need to give a command, wait, and then give another one when it prompts me. 我也尝试过使用带有命令列表的文件,但是它并不能解决我的问题,因为我基本上需要给出一个命令,等待,然后在提示时再给出一个。 This is what I found in the PuTTY FAQ and basically what I would like to try: 这是我在PuTTY常见问题解答中找到的内容,基本上是我想尝试的内容:

Probably your best bet is to use Plink, the command-line connection tool. 最好的选择是使用命令行连接工具Plink。 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. 如果您可以将Plink作为第二个Windows进程启动,并安排您的主进程能够通过管道将数据发送到Plink进程并从中接收数据,那么您应该能够从程序建立SSH连接。 This is what CVS for Windows does, for example. 例如,这就是Windows的CVS所做的。

EDIT: Based on the answer from user2460798, I have tried out the solution below. 编辑:基于来自user2460798的答案,我已经尝试了下面的解决方案。 I'm hoping to use PuTTY and be able to send commands to it that way. 我希望使用PuTTY并能够以这种方式发送命令。 My problem now is that I don't know how to send commands to the PuTTY session that gets opened. 我现在的问题是我不知道如何向打开的PuTTY会话发送命令。 So basically this code opens up a PuTTY session and tries to write the "ls" command to it, but nothing happens. 因此,基本上,这段代码打开了一个PuTTY会话,并尝试向其写入“ ls”命令,但没有任何反应。 I have no idea where the "ls" text is going. 我不知道“ 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")

Not sure to understand your question, but here is the way I use plink 不确定是否理解您的问题,但这是我使用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

It's not clear to me if you really want something that's non-interactive. 我不清楚您是否真的想要非交互性的东西。 The sentence 'It says "non-interactive command", which is what I'm using.' 句子“它说的是“非交互式命令”,这是我正在使用的命令。” sounds like you want non-interactive. 听起来像您要不互动。 But " I basically need to give a command, wait, and then give another one when it prompts me" sounds interactive. 但是“我基本上需要发出命令,等待,然后在提示我时再发出一条命令”听起来很互动。 Since it's not clear I'll answer for the interactive case. 由于不清楚,我将为交互式案例回答。

The second quote from the FAQ ("Probably your...") can be achieved using the code given here: How to run interactive commands in another application window from powershell , but replacing cmd.exe with plink.exe and changing the command line arguments as needed for plink. FAQ中的第二个引号(“可能是您的...”)可以使用下面给出的代码来实现: 如何从powershell在另一个应用程序窗口中运行交互式命令 ,但将cmd.exe替换为plink.exe并更改命令行plink所需的参数。

But sure to read the text that precedes the code. 但是请务必阅读代码之前的文本。 It mentions a couple of caveats. 它提到了一些警告。

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

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