PuTTY Tools and Tips

These tools and tips are useful to users of PuTTY, the awesome terminal emulator and secure-shell client for Microsoft™ Windows™.

Send-to Host Context Menu

This trick lets you right-click on any file (or multiple files) or folder, click Send To, then select server1 and have the files magically transferred to your home directory on the target ssh server. You can even put the shortcut on your desktop and drag files onto it to send them.

This effect can also be achieved using things like WinSCP, but sometimes you don't want to have to choose the host within WinSCP every single time you do this, and hard-coding a few hosts can boost your efficiency. This works best when you've setup key-based authentication.

  1. Install PuTTY and pscp in particular.
  2. Save (right-click and select "save link/target as") this batch file somewhere, perhaps where you've installed PuTTY.
  3. Copy the batch file to reflect the hostname, something like sendto.server1.bat
  4. Edit the batch file and change the value of HOST to your hostname, eg:
    set HOST="server1"
  5. In Explorer, right-click on the batch file and choose copy.
  6. Click on Start, choose Run... and type "%USERPROFILE%\sendto" or "shell:sendto".
  7. Right-click in the Sendto folder and choose Paste Shortcut.
  8. Rename the shortcut to something like Server1.

There is another way to do this using VBScript, that you might prefer.

Desktop Network Drive Icon

  1. On your desktop, place a copy of the shortcut created in the previous section, and rename it to something like Send to Server1.
  2. Right-click on the shortcut and change the icon to look like a network drive.
Now, right-click on a file somewhere, choose Send To and you should see your servername. After selecting it from the menu, your file(s) should start transferring. Alternatively, drag the file(s) to the shortcut you made on the desktop.

A ssh:// URL Type

Sometimes it's useful to have a webpage of links like ssh://server1/ where you can click to whatever you need to get to in a hurry. I often do this on Wikis I use for support documentation, so that I can quickly link directly to the host in question from the doco that talks about it.

This trick is achieved by creating a registry entry pointing to the PuTTY binary. The only problem is, when putty is run this way, it can't extract the hostname from the string it's given, because it's run like this:

  putty ssh://server1
instead of
  putty server1
PuTTY can't handle the URL prefix and so we need to work around this limitation. There are two work-arounds I've used to clean up the URL for PuTTY, both described below. The former is now my preferred strategy.

Command-line Magic Method

In this work-around, we use a bit of command-line magic to strip off the ssh:// part. The command line invocation looks like this:
  cmd /V:ON /s /c set url=%1 && set url=!url:ssh:=! && set url=!url:/=! && start "" "%%ProgramFiles%%\PuTTY\putty.exe" !url!
This runs a command prompt which sets the "url" variable to contain the cleaned-up server name (and port), and then runs "PuTTY" with the value. You could either put this string into the appropriate registry key (escaping double-quotes with backslashes), or you could simply download and import the SSH URL registry file. The same trick can work for rdp:// URLs, and I have an RDP URL registry file for that too. Instructions:
  1. Download the registry file (link in previous paragraph).
  2. Open in Notepad and review its contents to ensure that I'm not planting malware.
  3. Rename the file to have a .reg extension.
  4. Double-click the .reg file.
Credit: This method is based on a blog comment by Lonnie Huchinson. Nice one Lonnie!

Batch File Method

In this work-around, we use a batch file that can strip off the ssh:// part. To implement this solution:
  1. Save the batch file into the folder where PuTTY is installed, and rename it as putty.url.bat. Check that it has the correct path to the putty.exe binary.
  2. Save the registry file somewhere, open it in notepad to check that I'm not giving you a malicious registry entry, rename it to putty.url.reg, double-click it and answer the questions in the affirmative.

The Result

At this point you should be able to click on an SSH link such as this one: ssh://fred@localhost/. This link is unlikely to connect but you should get a "Connection refused" error from PuTTY when it tries. Now, try Start → Run... and type ssh://user@servername and see if it works for you.

Transparent Printing

Also known as ANSI printing or remote controlled printing, this feature is available on many terminal emulaters, including (of course) PuTTY. It's like you're printing through a reverse tunnel. There's some information about this feature in the PuTTy documentation.

Transparent printing works by having the remote (eg UNIX) server send a couple of escape sequences to the emulator. The first code \e[5i tells the emulator to switch to remote printing mode. The second code \e[4i tells the emulator to turn off remote printing mode. Any bytes send between the two escape sequences are sent to the configured printer.

To enable transparent printing, you need to tell PuTTy which printer to send the traffic to. See the documentation for how to choose this. The byte stream must be in a format that your printer can understand. For example, you could send a Postscript file to a Postscript-capable printer by simply embedding it between the escape sequences.

On most UNIX systems, you can generate the escape sequences in a device-independent manner using the "tput" command, and specifying the "mc5" and "mc4" terminfo codes. Here's an example:

$ tput mc5; cat ~/my-document.eps; tput mc4
On some systems, terminfo might not be setup with these codes. So an alternative is to use printf, like so:
$ printf "\e[5i\n"; cat ~/my-document.eps; printf "\e[4i\n"
If this is bit much for you, there's a tool called "ansiprint" that can take care of all of this.

Some printers will automatically detect the type of data being printed, and switch to Postscript or PCL accordingly. If the input stream is neither format, a printer just might treat it as text.

If your printer supports "raw" input on TCP port 9100, you can install the Windows printer drive to use a "Standard TCP/IP Port", using "Raw" protocol. You might like to install an extra copy of your printer's driver in this way, and name it "ansiprint". Then configure PuTTy to print to that printer.

This is how I print the hosts file on one of my systems:

$ ansiprint < /etc/hosts

No doco, no warranty, feedback welcome.