Run multiple commands from a file after logging into PuTTY from a bat file

batch fileputtyterminal

I have written a bat file in which I have the following:

putty.exe -t -load "ABC" -l username -pw password -m "C:\Users\pathasai\Desktop\abc.txt"

In my abc.txt I have written some commands which I want to be executed on PuTTY itself, but when I run the bat file, I am not able to run the commands. I am getting the error command not found. For instance, let us say our abc.txt has just one command: pbrun. It is saying command not found. (I get this error on the putty terminal.)

How do I get multiple commands to run one after the other from the abc.txt file?

Best Answer

run multiple commands from a file after logging into putty from a bat file

You can't log-in to PuTTY. PuTTY is an SSH client application which has no authentication.

You can use PuTTY to log-in to a server computer which is providing an SSH service. Typically this would be a Linux server. It is the server that requires you log-in to it.

In my abc.txt I have written some commands which I want to be executed on putty itself

You cannot run arbitrary commands on PuTTY itself.

You can use PuTTY to run commands in a shell on the server.

Those commands must be present on the server and supported by the server's operating system.

our abc.txt has just one command: pbrun. It is saying command not found.

That means that the command pbrun is not a valid command on the server's operating system (this is nothing to do with PuTTY).

Not all Linux servers have pbrun:

$ man pbrun
No manual entry for pbrun
$ pbrun
-bash: pbrun: command not found
$ uname
Linux
$

You must also be careful with line-endings on your commands. Windows files edited with notepad are likely to have lines ending with the two ASCII control characters Carriage-Return (CR or Control-M or ^M) and Line-Feed (LF or Control-J or ^J). I would expect PuTTY to take care of this, but it may not (I have not tested this).

$ pwd
/
$ pwd^M
: command not found
$

Update

I used Notepad to create this file, "commands.txt":

echo "this command works"
echo
echo "this command works too"
sleep 20 # so I can see

I used this command:

C:\temp>"C:\Program Files (x86)\PuTTY\putty.exe" rgb@server -m commands.txt

I got this result:

this command works

                  this command works too

I imagine there is some issue with stty or PuTTY settings affecting newline operation, but basically the -m mechanism works and, with some diligent effort, I would expect to be able to make it do useful work correctly.

Related Question