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.
Because the commands that are contained in the command.txt
are executed by a master shell one-by-one.
So the master shell executes the sudo
, waits for it to exit, before it proceeds with the other commands (ls
and cd
). And the sudo
does not exit (at least not on its own).
While you want the ls
and cd
to execute within a child shell executed by the su
.
You have to tell the su
to execute the commands.
This should work:
sudo su - -c "ls ; cd directory" user1
or this
echo "ls; cd directory" | sudo su - user1
Though I expect that you actually want to continue working in the shell. While the above will exit once the commands are executed.
So you want to add a shell to the list of commands:
sudo su - -c "ls ; cd directory ; /bin/bash" user1
or
echo "ls ; cd directory ; /bin/bash" | sudo su - user1
Best Answer
PuTTY is GUI application, not a console application. You cannot use input/output redirection with a GUI application.
There's no way to execute a command on the server automatically with PuTTY over a serial connection.
Though you should be able to use Plink (PuTTY command-line connection tool).
Plink is an equivalent of PuTTY, except that it is a console application, so you can use input/output redirection with it:
Though as your "bat" file is actually not a batch file (that would produce the commands for the device), but a text file that directly contains the commands for the device, you want to use the contents of the file as an input, rather than output of its execution: