To execute commands automatically using PuTTY, use the -m
switch to pass a text file with the command(s) to run, like:
putty.exe -ssh user@host -m commands.txt
Where the commands.txt
would contain
usrun -u root rush
But this won't help you with the password as that's an input to the usrun
command, not a command on its own, so the -m
is useless here. I do not know the usrun
, so I cannot tell if it has a way to "authenticate" any other method instead by "typing" a password. In general, whenever your are trying to automate password typing, you are doing things wrong. It's a security risk. Always check if there is a better (=safer) way.
Another issue are further commands. If all commands you want to execute are to be executed in the same environment/shell, you can put them to the commands.txt
line by line.
command1
command2
Though note that not all SSH servers do support multiple lines. Majority does though. If not, you can always use your server/shell-specific way to execute multiple commands on a single line like:
command1 && command2
But if the further commands are actually subcommands of the first command, what is the case with commands like su
, sh
, bash
, ssh
, ftp
, etc (and might be the case with the usrun
), the further commands are actually an input to the first, not standalone commands of the top-level environment/shell. It's actually an identical problem to the password input problem above.
Contrary to the password problem, with subcommands the primary command may have another mechanism to provide them instead of using input.
Having that said, you can use Plink (PuTTY command-line connection tool) instead of PuTTY. It's a tool from PuTTY package that works like PuTTY, but it is a console, not GUI, application. As such it can use input/output redirection. So you can redirect even a password or subcommands of the primary command from a file, as if you have typed them on a console. And anyway, the Plink is the tool to automate tasks, not PuTTY.
echo usrun -u root rush>input.txt
echo password>input.txt
echo command_for_usrun>input.txt
plink -ssh user@host < input.txt
Alternatively use KiTTY with its -cmd
switch that does what you want. The KiTTY is a GUI application (it's a clone of PuTTY). Its -cmd
switch basically simulates typing, so it does not have the problem with inputs (password and subcommands) to commands. But I found it rather unreliable. I do not really recommend it. But you may find it sufficient for your task.
I don't know how to use
-m
in syntax and what commands I should use in the notepad file. – Umang – 2016-07-22T06:56:38.683Unix and Putty and Batch? Forgive me I haven't used Putty in a while, and thank goodness. It was a tool I used in school when it was forced on me, but now I use much better tools. I digress. If it's batch you are seeking, then you are talking Windows. I'd suggest a Windows tool that uses Windows root https://git-scm.com/download/win . There are many, but this one is right up there with Putty in it's comparison, but walks with Windows much nicer. "Linux" commands, but on a Windows and Windows' directory.
– ejbytes – 2016-07-22T07:00:50.227@ejbytes : Thanks mate for your response. Looks like I was failed to explain my exact case. Let me just tell you what I am planning here. I have a Putty with me through which I connect the Jump server (which is Unix). I am having Window system through which I'll use Putty. So, to connect this Jump server, I am planning to make a
.bat
file. Make sense? P.S; I do not hold any background on making such batch file. I am new to this stuff. :( – Umang – 2016-07-22T07:04:25.343