3

I need to be able to create a process on a remote device and send come commands to it using a PowerShell script. 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. 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):

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.

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:

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.

EDIT: Based on the answer from user2460798, I have tried out the solution below. I'm hoping to use PuTTY and be able to send commands to it that way. My problem now is that I don't know how to send commands to the PuTTY session that gets opened. So basically this code opens up a PuTTY session and tries to write the "ls" command to it, but nothing happens. I have no idea where the "ls" text is going.

$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")
Programmer_D
  • 601
  • 2
  • 15
  • 27
  • 2
    Please show your code and the error message(s) you received. – Ansgar Wiechers Nov 25 '13 at 08:15
  • @AnsgarWiechers Ok, I did. – Programmer_D Nov 25 '13 at 18:29
  • `plink` doesn't read from STDIN. You need to run the command either with a command string (`plink user@host "foo; bar; baz"` or with a command file `plink user@host -m commands.sh`. – Ansgar Wiechers Nov 26 '13 at 10:08
  • 1
    @AnsgarWiechers Actually plink does read from stdin and send it to the stdin of the process in the SSH session. And it also redirects stdout from the remote system to plink's stdout. Similarly for stderr. This is a line from a script I've had in production for a couple of years: ` $received = $textToSign| plink $fwSupportAcct@$signingHost -pw $fwsupport_password 2>&1 ` – Χpẘ Dec 06 '13 at 22:41
  • @Programmer_D in the code snippet in your post , , and all need to be filed in with your values. – Χpẘ Dec 06 '13 at 22:55
  • @Programmer_D You left out a large part of the script that I referred you to. In particular `$p.StandardOutput.read( $outBuf, 0, 4096)` is where the read of the output of the SSH session occurs. But the rest of the script that you left out is important too if you want your PS script to be able to interact with the remote device. – Χpẘ Dec 06 '13 at 22:58
  • @user2460798 I stand corrected. – Ansgar Wiechers Dec 07 '13 at 13:47
  • @AnsgarWiechers I made a mistake in what I said above. While stdin, stdout, and stderr are all redirected by plink, you still have to give it something to execute. In my comment above I forgot that I had a python script executed from .bashrc that accepts input when fwSupportAcct logs in - so in that case I didn't need to specify a command. But in the usual case you would. However you can do this for example: `"echo test 1 2 3; ls /bin\`n"|plink $fwSupportAcct@$signingHost -pw $fwsupport_password sh 2>&1` Then sh will accept stdin from what's piped into plink. – Χpẘ Dec 10 '13 at 06:10

2 Answers2

5

Not sure to understand your question, but here is the way I use 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
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1

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.

But sure to read the text that precedes the code. It mentions a couple of caveats.

Community
  • 1
  • 1
Χpẘ
  • 3,403
  • 1
  • 13
  • 22
  • I am following the approach from the link you posted. I have updated my question with some code I am now trying. – Programmer_D Nov 25 '13 at 18:30
  • Oh, and to be clear: I don't want to interact at all with the script. I want it to run and do everything for me. – Programmer_D Nov 25 '13 at 19:03
  • @Programmer_D "Interactive" doesn't mean it has to be *you* interacting with `plink`, but that *something* is interacting with the program (in this case your script trying to send commands to the `plink` process). – Ansgar Wiechers Nov 26 '13 at 10:06