20110118

Get Putty to handle SSH protocol on Windows

QUICK & DIRTY SSH PROTOCOL HANDLER


This allows Windows to handle the SSH protocol such as ssh://sshserver:2022

Add the following 2 files to the folder where you store putty - e.g. C:\Program Files\Putty\

To register SSH protocol in Windows, run:
ssh -reg
ssh.bat:
@echo off
set this=%~dp0

if %~1.==. goto HELP

if /i "%~1" equ "-r" goto Register

set host=%~1
set protocol=%host:~0,5%

if /i "%protocol%" equ "--reg" goto Register
set protocol=%host:~0,6%

:sshhere
if /i "%protocol%" neq "ssh://" goto ERROR

set sshHOST=%host:ssh://=%
set sshHOST=%sshHOST:/=%

Echo %sshHOST%
pause

:Open
Echo Starting...
start "SSH Host: %sshHOST%" /D"%this%" putty.exe -ssh %sshHOST%
Goto :EOF

:ERROR
msg * /time:10 "Invalid SSH URL - expected format is ssh://HOSTNAME"
Goto :EOF

:Register
reg import "%this%\SSHhandler.reg"  
reg add HKCR\ssh\DefaultIcon /ve /d "%this%PUTTY.EXE,1" /f>nul 
reg add HKCR\ssh\shell\open\command /ve /d "%this%ssh.bat \"%%1"" /f>nul 

Goto :EOF

:HELP
Echo Syntax: 
Echo Script Path: %~dpnx0 
Echo.
Echo -r OR --register
Echo Add this script as SSH Protocol handler in Windows Registry
Echo.
Echo ssh://[username@] 
Echo Runs putty.exe -ssh [username@]
Echo.

Goto :EOF

SSHhandler.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\ssh]
@="\"URL:SSH Protocol\""
"URL Protocol"=""

[HKEY_CLASSES_ROOT\ssh\DefaultIcon]
@="C:\\Program Files\\putty\\PUTTY.EXE,1"

[HKEY_CLASSES_ROOT\ssh\shell\open\command]
@="C:\\Program Files\\putty\\ssh.bat \"%1\""