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!
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.
In the left pane, expand the HKEY_CLASSES_ROOT hive. Right-click in the right pane and choose New > Key. Name the newly created key SSH and press enter.
Again in the right pane, double-click the default string value. Enter URL:SSH Protocol and click OK to change its value data. Right-click and create a new text string value named URL Protocol. Leave its value data blank.
Right-click again in the right pane and create a new key named shell. Continue creating new sub-keys until you have a directory structure that looks like this:
Be sure that the command sub-key is selected in the left pane. Double-click its default value and change the value data. You want to provide the command line that should be executed for this custom protocol. In this example, it should launch the script file that we will be creating:
wscript.exe “C:\SSH\ssh.vbs” “%1”
The quotation marks are optional unless your path contain spaces. I’ve included them here for demonstration purposes. Likewise, the quotation marks around the %1 are also optional in this case since valid URLs cannot contain spaces.
By the way, the %1 instructs the browser to append the URL data to the command line we’ve provided. If you simply wanted to launch an application without passing any data to it, you could leave that out.
Now let’s create a script that will parse the URL from our command line into a valid host name for the PuTTY application.
Here’s where the problem lies. Entering the URL SSH://hostname into the address bar in Firefox actually passes the following URL to command line provided in our registry settings:
There are two items of concern here. The first is that the browser has included the SSH protocol identifier in the URL. The second is that some browsers, including Firefox, append a trailing forward slash to all URLs. We’ll need a script to remove both of these elements before launching the SSH client.
strSSH = "C:\ssh\putty.exe"
Begin your WSH script by providing a path to the SSH client. If you’re using a client other than PuTTY, be sure to include any necessary command line switches as well.
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
Next, our script needs to read the URL that has been passed to it via the command line and store it in a variable. I’ve also added a little error-handling that will quit script execution if there was no URL.
Set WshShell = CreateObject("WScript.Shell") WshShell.Run """" & strSSH & """ """ & CleanHostname(strHost) & """" WScript.Quit
We finish our script by telling it to launch the SSH client. I’ve used the WshShell object’s Run method to execute a command line. Notice that I’m passing our URL through the CleanHostname function. This is the function that will strip the URL string into a valid host name.
Finally, we add the CleanHostname function.
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 'Return cleaned hostname CleanHostname = strHost End Function
This function performs the two steps I mentioned earlier. It removes the protocol from the beginning of the URL and removes a trailing forward slash if one exists.
Save the file as ssh.vbs and make sure that it is located at the path specified by the command line in the registry key you created earlier.
Now open any browser and enter a URL in the form of SSH://nodename and watch PuTTY open a connection to the node name provided!
If you are not comfortable making the registry edits or creating the script discussed in this article, you can simply download the installer below to do it for you. The installer also includes the free PuTTY SSH client software.