«

»

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!

 

  • Cabe

    I’m running a shutdown system called mopUPS. I have two windows servers and a linux server. I created a batch file for the windows servers then i used plink to configure a command for the linux machine and then added the linux command to the batch file. Whenever i run the batch file by itself it works perfectly. But whenever i put the batch file into mopUPS command line it only runs the two windows commands and it gets caught on the plink command….. Any suggestions?

  • Karl Ave-It Allinson

    Hi, I like the idea of a batch file to reboot some stuff on my network, do you think there is a way that a .bat file can be configured to run putty, input ssh login and the send a reboot command?

    • Hey! I don’t believe you can script Putty from a bat file but that’s
      what Plink is for. It’s basically a command line version of Putty that
      you can use in a batch script.

      • Karl Ave-It Allinson

        OK, great, and thanks for the speedy reply. I work for a wireless ISP in the uk. from time to time the access points become “stale”, for want of a better word. was hoping to come up with a script that can be executed for a number of different ip addresses so I can configure each script to double click, login with ssh and send the reboot command without having to ssh into each one everytime. It gets a bit tedious when you have 50 or 60 access points……