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?

  • I don't know how to use -m in syntax and what commands I should use in the notepad file. – Umang Jul 22 '16 at 6:56
  • Unix 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 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 Jul 22 '16 at 7:00
  • @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 Jul 22 '16 at 7:04

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.


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.

| improve this answer | |
  • Martin, Thank you so much for your detailed explanation. Much Much appreciated. Couple of questions popped up on your answer if you can help me on that, too. 1. Here how my .bat looks like. C:\Users_my_user_\Putty.exe -load "Jump_Server" -l username -pw password -m "Text file path containing commands" This successfully takes me to where I want. Now, technically, it should take the command that I have left in the text file but it is not taking. The text file contains only usrun -u root rush Any idea what is wrong here? – Umang Jul 22 '16 at 8:06
  • 2. I'll type the password manually - that's okay!! But what about the further commands? can I add them in the same text file? Thanks again. – Umang Jul 22 '16 at 8:06
  • Regarding the 2nd option you are talking about, it would be difficult to use that application since this is something I am not going to use for my personal use. Hope you get me:) Would be great if you can help in Putty only!! – Umang Jul 22 '16 at 8:11
  • Se my updated answer for further commands + If the further commands are subcommands of the usrun you cannot combine it with typing password (at least not with a simple command-line tools) – Martin Prikryl Jul 22 '16 at 8:16
  • + see my hint for kitty – Martin Prikryl Jul 22 '16 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.