Creating a Custom URL Protocol for SSH

August 9th, 2007
1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4 out of 5)
Loading ... Loading ...

It can sometimes be very useful to have links in your web browser spawn third-party applications for you. A user recently submitted a question wanting to have a URL open an SSH connection with PuTTY. As he learned, this can be done by creating a custom URL protocol.

I am trying to set up Firefox to launch a putty session upon the entry of ssh://nodename in the address bar and I am having some issues. I have successfully added the necessary keys to the registry such that putty is launched, however, putty doesn’t accept the input correctly and tries to connect to a non-existent host.

I was thinking about writing a bat file which would accept the input from Firefox […] but I am unsure how to do so since I am not a scripter or a windows guru. Any help you could provide would be greatly appreciated. Thanks!
- Kyle V.

Well, Kyle, you are so very close to your solution. The problem here is that the browser is passing the complete URL to the PuTTY application. Since PuTTY does not accept full URLs as host names, you receive a non-existent host name error.

As you suggested, the easiest way to solve this problem is to use a script to parse the URL into a valid host name and, in turn, launch the PuTTY application. You suggested a batch file, but you’ll see why this is much easier in WSH. But first, let’s show our readers how to create the custom protocol necessary to make this all work.

Adding custom protocols is extremely easy but it does require editing the Windows Registry. If you are uncomfortable editing the registry, I’ve written an installer that will do this for you. You can find the link at the end of this article.

You’re going to be creating several registry keys and values to create the key structure necessary for this to work. To begin, you’ll need to click the start button and choose Run… to open the Run Dialog box. Enter regedit and choose OK to open the Registry Editor.

Pages: 1 2 3 4

Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

5 Responses to “Creating a Custom URL Protocol for SSH”

  1. kavernon Says:

    Robert,

    Thanks so much for your help. The insight you gave into the nature of the issue along with the suggestions and ultimately a packaged software solution was exactly what I was looking for. Keep up the great work!

    Kyle

  2. Nilpo Says:

    Thank you so much, Kyle. It was a pleasure. I’m glad I could help!

  3. Tim Koopman Says:

    Hi,

    I made a small mod to your ssh.vbs script to enable it to accept custom ports. Did it quick and dirty could be done a bit better but works.

    Tim

    ‘ Region Description

    ‘ Name: ssh.vbs
    ‘ Author: Robert Dunham (Nilpo)
    ‘ Website: http://www.nilpo.com
    ‘ Description: This script is for use with a custom SSH protocol for Putty.

    ‘ EndRegion

    strSSH = “C:\ssh\putty.exe”
    strPort = “22″

    Select Case WScript.Arguments.Count
    Case 0
    WScript.Echo “No hostname provided. Aborting SSH operation.”
    WScript.Quit
    Case Else
    Set colArgs = WScript.Arguments
    For i = 0 To WScript.Arguments.Count - 1
    strHost = ” ” & WScript.Arguments.Item(i)
    Next
    End Select

    Set WshShell = CreateObject(”WScript.Shell”)
    strHost = CleanHostname(strHost)
    WshShell.Run “”"” & strSSH & “”" -P ” & strPort & ” “”" & strHost & “”"”
    WScript.Quit

    Function CleanHostname(strHost)
    strHost = Trim(strHost)
    ‘Remove protocol if it was passed
    If InStr(strHost, “ssh://”) = 1 Then
    strHost = Right(strHost, Len(strHost) - 6)
    End If
    ‘Remove trailing slash if present
    If InStrRev(strHost, “/”) = Len(strHost) Then
    strHost = Left(strHost, Len(strHost) - 1)
    End If
    If InStrRev(strHost, “:”) > 0 Then
    strPort = right(strHost, Len(strHost) - InStrRev(strHost, “:”))
    strHost = Left(strHost, InStrRev(strHost, “:”) - 1)
    End If
    ‘Return cleaned hostname
    CleanHostname = strHost
    End Function

  4. Nilpo Says:

    Looks good, Tim. Thanks for the addition!

  5. LePiaf Says:

    Thank you for your article. It’s very good. I search very long time how to create my own protocol. I customise your script for my usage.

    LePiaf

Leave a Reply