Search code examples
shellsshputty

Automatically execute a command after PuTTY login and provide it with an input


I have a program running on a server. I need to send some specific input to that program. I tried to do that using PuTTY.

I am currently using:

putty.exe -ssh user@server -pw password -m command.txt

Where command.txt is the file containing the commands that I try to run on the server. The problem is the terminal closes instantly after the connection is made. Also, the commands I am trying to send are specific for that program, so they are not Linux commands. It's something like:

SomeName ENTER
SomePassword ENTER
SomeNumber ENTER

Is there any way to do that?


Solution

  • The file that you pass using -m switch can contain shell commands only. You cannot use it to provide an input to those commands. For that would would have to use remote shell features, like an input redirection – if you really need to use PuTTY.


    But in general, to automate command execution, you should use Plink (part of PuTTY suite).

    With Plink, you can do this:

    (
      echo input line 1
      echo input line 2
    ) | plink.exe -ssh user@example.com -pw password command
    

    Or an equivalent:

    plink.exe -ssh user@example.com -pw password command < `input.txt`
    

    Where the input.txt contains the input for the command:

    input line 1
    input line 2