PuTTy: Bulk import PuTTy session definitions into the registry using Powershell

Putty is one of the first tools I install on any host or jumpbox.  And creating a saved session definition is extremely helpful so I can get the right window size, scrollback, keep alives, color scheme, etc. but creating each session definition by hand is time consuming.

In this article, I will show you how to generate these session definitions for a bulk set of hosts, preloaded for a Windows PuTTy installation.

Overview

Ultimately, what shows up in the PuTTy session list is the list of registry entries at “KHCU\Software\SimonTatham\Putty\Sessions”.  We will use a template registry file and Windows Powershell to create these in bulk.

This would suffice to create a pre-populated list, but we will take it one step further and also populate “KHCU\Software\SimonTatham\Putty\SshHostKeys” so that the host keys are preloaded and PuTTY will not have to prompt us to accept the key on the first access.  This will be done using a Python script that can convert a file in the known_hosts format to a registry file.

Once you have all these loaded into your registry, it is not necessary to have your co-workers run these same steps if they want the same list.  You can simply export your registry branch and have them import it.

Prerequisites

Install Putty

Download and install PuTTY with all defaults.

Install Git for Windows

Git for Windows contains a minimal bash and ssh-keyscan which is used to generate the known_hosts format.  Download and install Git for Windows with all defaults.

Install VC runtime

Python 3 on Windows has a dependency on the Visual C++ Runtime 2015.  You must use at least Update 1 to satisfy all dependenciesDownload and install VC runtime Update 1  with all defaults.

Install Python3

Download and install Python3 for Windows with all defaults.

Create list of hosts

The first thing we need is a list of the hosts that we want added to Putty.  The format we will use is a text file where each line represents a host, and each line has the format:

<name>,<IP|hostname>

The name is what PuTTY will show in its list, while the second field is the FQDN or the IP address it will actually use to connect to the remote host.  Here is an example:

myhost,192.168.1.10
mailhost,mailhost.subdomain.com

Create a text file named “puttyhosts.txt”, and whether you populate this text file manually, or use a script that discovers each host is completely up to you.

Populate PuTTy session list

Download createPuttySessions.ps1 and template.reg from my github project and put it into the same directory as the “puttyhosts.txt” you created in the last step.

Drop down to the command line, and run:

powershell -executionpolicy bypass .\createPuttySessions.ps1

This will go through each line in your “puttyhosts.txt” and use the “template.reg” as a blueprint for creating registry files that are then imported using the standard Windows utility reg.exe.  Close PuTTy and reopen it, and now you should see all your host sessions listed.

My template.reg is very basic with a 80×45 terminal size, 10k line scrollback, and 30 second keepalives.   You can always formulate your own template settings by modifying a session in the PuTTY GUI, then using regedit.exe to export the registry key from “KHCU\Software\SimonTatham\Putty\Sessions\<yoursession>”

Populate host keys (optional)

You might notice that the first time you open a session to one of these hosts, you get a GUI popup that asks you to accept the host key.  This might not bother you, but if you want to pre-populate the known host keys and avoid this alert, these keys are stored in the Windows registry at “KHCU\Software\SimonTatham\Putty\SshHostKeys”.

The easiest way to get these into the registry is using the kh2reg.py Python script in the ‘contrib’ directory of the PuTTy project.  This script was originally developed for Python 1.5, so it should still run on Python2, but for this article I went ahead and converted it to Python3.

The kh2reg.py3 script takes a file in the known_hosts format and converts it to a Windows .reg file that can then be imported.   The reason why the Windows Git client was listed as a prerequisite for this article is because it contains the ‘ssh-keyscan’ utility which can connect to a remote host and outputs the host keys in the known_hosts format.

So, once again we will iterate through the puttyhosts.txt file you created, and for each host we will use ssh-keyscan to retrieve the keys in known_hosts  format.  Then we run kh2reg.py3 to convert this to a registry file that is imported.

Download kh2reg.py3 and scanHostkeys.ps1 from my github project and again place them into the same folder as your puttyhosts.txt.  Before going on, make sure python3 and ssh-keyscan are available on the Windows PATH.

> python.exe --version
> ssh-keyscan.exe --version

Then scan all the hosts in puttyhosts.txt to retrieve the keys:

powershell -executionpolicy bypass .\scanHostKeys.ps1

The population of the keys can be verified in the registry at “KHCU\Software\SimonTatham\Putty\SshHostKeys”, and when you open the session from PuTTY, you should not get any prompt.

 

 

REFERENCES

https://www.gngrninja.com/script-ninja/2016/3/14/list-your-putty-sessions

https://stackoverflow.com/questions/13023920/how-to-export-import-putty-sessions-list (if you have option to export/import entire registry branch)

https://git-scm.com/download/win (Git for Windows includes ssh-keyscan)

https://git.tartarus.org/?p=simon/putty.git;a=blob;f=contrib/kh2reg.py;hb=HEAD (Original kh2reg.py for Python1.5, converts known_hosts format to .reg)

https://github.com/ContinuumIO/anaconda-issues/issues/443 (tells about Python3 needing VC runtime, actually must be at least Update 1)

https://www.microsoft.com/en-us/download/details.aspx?id=49984 (VC Runtime 2015 Update 1)

https://www.python.org/downloads/windows/ (download python 3.x for Windows)

https://www.cyberciti.biz/tips/putty-session.html (transferring registry)

http://man.openbsd.org/ssh-keyscan.1 (*nix ssh_keyscan)