Like many people in IT, I find myself working with several types of operating systems on a daily basis. For example, I need to have a Windows workstation, but I spend nearly all of my time working on Linux servers. Given this setup, I wanted to find the most convenient and effective way to access and automate from my Windows system. PuTTY to the rescue!
What is PuTTY?
PuTTY is a free terminal/SSH client for the Windows platform. Along with the main client, several utilities are available which emulate various GNU/Unix programs:
- PuTTYtel – Telnet client
- PSCP – Secure file copy
- PSFTP – Secure file browsing and copying
- Plink – Command line version of PuTTY
- Pageant – SSH authentication agent
- PuTTYgen – SSH key management
PuTTY Connection Manager
PuTTY Connection Manager (or PuTTY CM for short) is wrapper built to provide tabbed PuTTY windows, as well as connection databases and login macros. It is difficult to describe how useful this program is for people who work with a large number of servers on a daily basis. All I can say is to go try it out and see for yourself. The connection database is stored with a DAT extension, but is actually an XML file and can easily be modified with a text editor. For security, you also have the option of encrypting the database.
Setting Up PuTTY on Your Machine
First, download the PuTTY utilities that you would like from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html. You may download the utilities individually or download the ZIP file containing all the utilities. Move or extract the files into your C:\Windows directory. This will place all the programs in your system’s path, so that you can easily reference them on the command line.
The PuTTY CM tool may be difficult to find online, but it’s out there. When you get the executable, run it first from one of you user directories (e.g. My Documents). Then move the executable and libraries to C:\Windows. You can also just leave it in the user directory, as it is not used from the command line and doesn’t actually need to be in your system’s path.
Using PuTTYgen and Pageant
Public key authentication is a very secure and convenient way to handle SSH connections, especially when scripting. I will not get into much detail with public key authentication here, but there are many great resources online that can explain it in more detail for beginners. My purpose with this section is to explain that PuTTY allows for simple generation and deployment of both SSH.com and OpenSSH keypairs. Most Linux systems will use OpenSSH keys, while many other Unix-like systems use SSH.com. If you do not already have one, use the PuTTYgen tool to create a new keypair:
- Open the PuTTYgen utility
- Set the number of bits in a generated key to 2048
- Click “Generate”
- Move your cursor around the blank area until enough entropy is generated to create the private key
- Enter an encryption passphrase for the newly generated key (optional, but highly recommended)
- Click the “Conversions” menu item, and export as an OpenSSH key
The public key displayed in the window should go into the .ssh/authorized_keys file in your home directory of every system for which you would like to use public key authentication.
At this point, you have two choices on how you can use public key authentication. You can either load the key with each new PuTTY session, or you can use Pageant to store the key in memory and use it for all future PuTTY sessions. With the first option, you have to decrypt the private key each time you log into a system. With Pageant, you only have to decrypt the private key once, to load it into memory.
The most convenient way to use Pageant is to have it automatically start and load the private key when you log in to your system. To do this, open the “Startup” directory in your start menu, and create a link with the target:
C:\Windows\pageant.exe C:\<path to your key>\yourkey.ppk
Each time you log in to Windows, you should be prompted to enter the passphrase for your key, after which it will be loaded into memory for all the other PuTTY tools to use. If you log out or shut down your computer, the key will remain safely encrypted on your hard drive.
Scripting with PuTTY
PuTTY scripting can easily be done with Windows PowerShell. To start using PowerShell for scripting, you must update your policy to allow PowerShell scripts to be executed. To do this, open up a PowerShell session as an administrator, and type in “Set Execution-Policy Unrestricted”. PowerShell will ask for a confirmation, after which you will be able to execute scripts.
Once you are able to execute scripts, get to work! I generally like to do as much logic as possible in PowerShell (taking advantage of objects and .NET libraries), then execute as few commands as I have to on the remote Linux servers. If you prefer or need to run complex shell statements and scripts on the Linux machine, you can use HERE documents or even just upload and run the script on the server. It’s really up to you. Here’s some things I have done:
- GUI based JBoss deployments (backup/deploy multiple WAR files between environments or your workstation with just a few clicks)
- JBoss/Tomcat container restarts… great for middle-of-the-night issues
- Specialized content deployment
- Retrieving and extracting data out of XML files
Hopefully this has helped generate some ideas on how you can automate some of your more repetitive tasks and frees you up to tackle bigger things. The important thing to remember is that running a Windows workstation does not mean you can’t easily work with Unix/Linux systems!