Wednesday 7 December 2016

Open a putty session using batch file.

In my experience i used to have a lot of Ubuntu machines and daily i used to check some details on all the machines.

If u do this manually then u need to open a putty sessions individually and give login and password and then u need to give the commands u required.

In my case i have 10 Ubuntu machines, i need to open them through putty and do a cd to certain path and run some commands on each machine.

So i automated the complete thing.
Now just what i do is just do a double click on the "Batch file" and it will do my all tasks.

What i did in Batch file is:

putty.exe username@example.com -pw password -t -m commands.txt

What the -m does is, that it makes PuTTY instruct the SSH server to start that command(s) INSTEAD of a shell. So once your command finishes, so does the session.

If you want to run the shell after the cd command, you need to add it explicitly to your cmd.txt, like:

cd /my/path ; /bin/bash

Also the -m implies "nopty"/non-interactive mode. To use an interactive shell you need to override that using the -t switch.

putty.exe -ssh example.com -m "c:\path\cmd.txt" -t

Now i just need to add 10 such commands in a batch file with respective usernames, passwords and commands. Then just save that file on desktop and double click on it for execution.

This make my job easy.