4

I have created a .bat file which automatically takes the SSH server (Jump Server) access. Now, I have couple of commands which I would like to hit in that window.

For an ex. (To take root access)

usrun -u root rush

With this command, it'll ask me to enter the password.

So, how can I achieve this entirely through .bat file?

0

1 Answer 1

4

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

You also might want to add -t:
Plink using PuTTY colour scheme


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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .