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")
8
  • 2
    Please show your code and the error message(s) you received. Commented Nov 25, 2013 at 8:15
  • @AnsgarWiechers Ok, I did. Commented Nov 25, 2013 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. Commented Nov 26, 2013 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 ` Commented Dec 6, 2013 at 22:41
  • @Programmer_D in the code snippet in your post <password>, <username>, and <device> all need to be filed in with your values. Commented Dec 6, 2013 at 22:55

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

I am following the approach from the link you posted. I have updated my question with some code I am now trying.
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 "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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.