Bash SSH Happiness

So Dennis? (whose name doesn’t appear anywhere on his blog except for in the URL) wrote a cool bash script to create aliases for every host in your ~/.ssh/known_hosts file. I found some bugs/problems so I spiffed it up a bit. As the comments say, you should pipe the output to sort(1). I’ve tried to make the text as small as possible, but it won’t all display. I tested selecting it and even the parts that don’t display end up in my clipboard, so there ya go.

#!/bin/bash
shopt -s extglob
isint(){
    case $1 in
        ?([-+])+([0-9]) )
            return 0;;
        *) return 1;;
            esac
}

if [[ -d ~/.ssh ]]; then

    # Touch files we expect to exist
    if [[ ! -e ~/.ssh/config ]]; then touch ~/.ssh/config; fi
    if [[ ! -e ~/.ssh/known_hosts ]]; then touch ~/.ssh/known_hosts; fi

    # Parse ~/.ssh/known_hosts and ~/.ssh/config to find hosts
    for x in `sed -e 's/[, ].*//' ~/.ssh/known_hosts; awk '/^Host [^*?]+$/{print $2}' ~/.ssh/config`; do

        # Don’t override commands
        type "${x}" > /dev/null 2>&1 && continue

        # Remove the domainname
        y=${x%%.*}
        # you don't want IP addresses for aliases, trust me.
        isint $y && continue

        # If it's a short-name, move on
        #z=${x##*.}
        #[[  "${z}" == 'edu' ||  "${z}" == 'com' || "${z}" == 'net' ]] || continue
        # So the above is commented out because you'd be surprised at how much 
        # you rely on your search path. You should pipe the output of this script to
        # sort and your fqdn's will override your shorts.
        echo alias "${x}"=”ssh $x”

        if [[ "$y" != "$x" ]]; then
            if ! type $y > /dev/null 2>&1; then
            echo     alias $y=”ssh $x”
            fi
        fi
    done
fi

Dennis also mentions that you might want to add HashKnownHosts no to your ~/.ssh/config file. If you have some hashed keys in your file, you should remove them before running this script. Sadly I couldn’t find any way to programmatically convert a hashed file into a non-hashed file. But to make up for it, here’s a bonus alias!

# removes _exactly what you type_ from ~/.ssh/known_hosts
# meaning 'grapes' gets you the key for 'grapes' vs 'grapes.wrath.com'
alias forget="ssh-keygen -R"

OSCON08 Wrap-up

A few things to wrap up.

  • The Stress on Programmers Slides Man oh man I wish there was a recording of the talk. I looked over the slides and they don’t do the talk justice by themselves as one might expect.
  • Glenn posted some comments that I think have the slides for the XMPP talk I saw.
  • OneLessMac I didn’t buy a sticker, but I love the idea and think it’s great that I’m not the only one.
  • Last.FM I’m notoriously slow to new stuff, so take this with that. I don’t get it. I mean I sorta get the basic concept, but it doesn’t seem all that useful or interesting.
  • I said I was a bad whinger. After further thought, I don’t think that’s true. I think I more often than not try to present a solution to whatever it is I’m unrelentingly complaining about. :)