Saturday, June 16, 2012

SSH bookmarklet

When developing server software for a company, you might be troubleshooting an issue on a development server other than your own and you want to login to that server. Specifically I'm talking about when you are looking at say a QA person's issue in a web browser and you want to login to their box and see what's wrong.

I found out recently that my MacBook has the ssh protocol registerred already where if you click on a link with this scheme: ssh://user@host.name, then that executes ssh user@host.name. I wanted to figure out a way to utilize this in a bookmarklet. I also wanted it to be easy enough such that you could enter a different username, like root or the QA person's username if you have certain access restrictions on that server.

Here is my attempt to create that bookmarklet. Just drag it to your browser's toolbar:

Run SSH

Here is my breakdown of what happens:
// prompt for a user to ssh as, save that in the window for next time
window.sshuser = prompt('Login to ' + window.location.hostname + ' as', window.sshuser || '');

// construct the user@ string
user = (window.sshuser ? window.sshuser + '@' : '');

// construct the url and execute it
window.location = 'ssh://' + user + window.location.hostname


This works great, but I really want to figure out how to make multiple programs that register the ssh:// protocol for any operating system or browser. Something like this link explains: http://kb.mozillazine.org/Register_protocol

Windows is pretty easy to create a batch script that adds registry keys for an ssh protocol to run putty (or even cygwin ssh). I would like to know how to do the same on Ubuntu (or any Linux). I'm pretty stoked that the Macbook already has it. I don't know if its related to me installing mac developer tools, but I'm pretty satisfied.

Anyway, Hope this bookmarklet helps!

3 comments:

  1. I tried poking at this a few years ago at AirWave but didn't poke hard enough; those telnet/ssh menu links never worked for me.

    The basic technique is to associate some MIME type with some ssh program. A quick search turns up http://www.schooner.wail.wisc.edu/docwrapper.php3?docname=ssh-mime.html which might be what you're looking for.

    ReplyDelete
  2. Thanks for the link dbkliv. I looked at this on my firefox browser using Ubuntu and when I click the bookmarklet, the browser says: "This link needs to be opened with an application. Send to" And shows a program selector.

    From this, I just created this script in my home directory:
    #!/bin/sh
    location=`echo $@ | sed 's,ssh://,,g'`
    echo $location | egrep '^([a-zA-Z0-9]+@)?[a-zA-Z0-9\.]+$' || exit "Bad SSH string $@"
    xterm -e ssh $location

    This works just fine. Since I'm using the KDE window manager, I changed xterm to konsole to get my personal console settings.

    ReplyDelete
  3. Running the bookmarklet on chrome asked me to use xdg-open ssh://myhost

    This seems to do exactly what I want without using the dumb shell script I made.

    ReplyDelete