Jun 29 2012

SSH Via Command Prompt Or Batch File In Windows

I normally don’t leave all of my computers turned on through-out the night but sometimes turning them all off before I go to bed can be a nuisance. I was determined to create a batch file to turn off all of my computers. For windows it is easy but Linux and my ESXi server required me going in via SSH. I accomplished this by using a utility called Plink.

Installing Plink

Unfortunately there is no native SSH program built into Windows. To compensate for this we can use a program called Plink from the makers of the all awesome Windows SSH client, PuTTY.

What makes Plink different from PuTTY is that it is completely operated from the command prompt within Windows. This allows us to send SSH commands through a terminal or scripted in a batch file.

You can grab the latest copy of Plink from its download page here.

Once you have downloaded Plink move it to C:\Program Files (x86)\plink\plink.exe   *Anywhere is file, just remember its location…

Now we will add Plink to the Windows path. Although this is not required, it simplifies executing ssh commands in the command prompt later.

To add Plink to the Windows path right click on My Computer and select properties. Under the Advanced tab click the Environment Variables button. In the bottom section find the entry for PATH. Highlight it by clicking on it and then click edit. At the very end append “;C:\Program Files (x86)\plink” without the quotes. Click OK in the windows to confirm these changes.

Now Plink is accessible in the command prompt by typing “plink” (without the quotes) in any directory!

You can learn how to use the vast amount of features Plink contains simply by typing “plink” at a command prompt. You can also continue and see how simple it is to connect to a remote computer via SSH at the Windows command prompt or by using a Windows batch file.

Logging Into A Server Via SSH With Plink

Using Plink to log into a server via SSH can be very easy it high security isn’t an issue or it can be a bit more complicated. I say this because, when using a batch file, you can log into a server from a command prompt by appending the password to the Plink connect line.

To connect to a server via SSH with Plink type the following at a Windows command prompt. Replace {SSH  USERNAME} with you username and {SERVER IP} with its IP address.

plink -v -ssh {SSH  USERNAME}@{SERVER IP}

You will then connect to the server via ssh and be prompted for a password. After authenticating you can type away at the remote terminal.

Using A Batch File

A batch file sending SSH over the Windows command prompt can be very useful. Recently I set up a batch file to shutdown a Linux server as well as an ESXi virtual server over SSH. Regardless of what command you want to send, in my case halt, you will need to send the password for the server as well as the command all in one line when operating using the batch file.

To do this simply use the following command in your batch file.

Replace {SSH  USERNAME} with you username. Replace {SERVER IP} with the servers IP address. Replace {PASSWORD} with the the appropriate password. Replace {COMMAND} with the command you would like to send to the server.

plink -v -ssh {SSH  USERNAME}@{SERVER IP} -pw {PASSWORD} {COMMAND}

Now there is one problem with this. Your username and password will be sitting in plain text in a batch file. To fix this you want want to set up a Public key on the server for connecting to. This would allow you to not need a password at all to connect to the server.

That’s it!

Using Plink is a great simple way to communicate with a server over SSH using the Windows command prompt or a Windows batch file! Good luck, if you have questions – that’s what the comments are for!