How to type commands in PuTTY by creating batch file?

3

1

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?

Umang

Posted 2016-07-22T06:54:52.580

Reputation: 49

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.683

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 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

Answers

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

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.

Martin Prikryl

Posted 2016-07-22T06:54:52.580

Reputation: 13 764

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 – 2016-07-22T08:06:09.430

start="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 – 2016-07-22T08:06:23.743

    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 – 2016-07-22T08:11:33.110

    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 – 2016-07-22T08:16:20.000

    >

  • see my hint for kitty
  • < – Martin Prikryl – 2016-07-22T08:22:39.327

    Thanks Martin. Just saw your updated answers. Indeed, they were very helpful. Have further questions on this discussion. Hope you wont mind :)

    Now, I am taking SSH to one of the production servers (after taking the SSH to that Jump Server). In this server, I want to hit some commands. Any idea how can I achieve this? – Umang – 2016-07-22T09:32:31.553

    Now, I am taking SSH to one of the production servers - means what? – Martin Prikryl – 2016-07-22T10:55:02.983

    Please stop using vague phrases like "taking SSH access" - be specific. – Martin Prikryl – 2016-07-22T10:58:34.217

    I have a Jump Server (Unix) which I connect through Putty from the Windows machine. After connecting this Jump server, I connect the another UNIX server. I would like to perform some commands in that another UNIX server. Can I achieve this through .bat file? – Umang – 2016-07-22T11:03:38.357

    How do you connect to the another UNIX server? Are you using ssh command or what??? – Martin Prikryl – 2016-07-22T11:40:40.463

    Yes, Martin. I am using ssh command to connect the another UNIX server. – Umang – 2016-07-22T13:37:32.320

    And the problem is? The ssh command is covered in my answer too. – Martin Prikryl – 2016-07-22T14:28:09.470

    After taking ssh access to that another UNIX server, I have some repeated commands which I would like to hit in that server. How can I achieve this? Note:- This is the further process to what I have mentioned in my original question – Umang – 2016-07-23T09:19:32.907

    What's "repeated commands"? Anyway, this starts being complicated. Time you ask a new question. – Martin Prikryl – 2016-07-23T09:51:23.990