Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

ssh

putty

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?

like image 947
Liviu Pinzaru Avatar asked Oct 21 '25 04:10

Liviu Pinzaru


1 Answers

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 [email protected] -pw password command

Or an equivalent:

plink.exe -ssh [email protected] -pw password command < `input.txt`

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

input line 1
input line 2
like image 155
Martin Prikryl Avatar answered Oct 26 '25 16:10

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!