Friday, February 14, 2014

hostnames as commands

Several years ago, I adopted a practice I've realized I should write down. I have two shell scripts that live in ~/bin/:
james.mojo.home ~ $ cat bin/ssh-host
#!/bin/bash

start=`date`
remote_host=`basename $0`
if ! ssh $remote_host $*; then
    echo from $start to
    date
fi
james.mojo.home ~ $ cat bin/mosh-host
#!/bin/bash

start=`date`
remote_host=`basename $0`
if ! mosh $remote_host -- $*; then
    echo from $start to
    date
fi
And I have many symlinks in ~/bin/ that point to those scripts. For example:
lrwxr-xr-x 1 moquist staff 8 Jul 12 2013 aristotle -> ssh-host
lrwxr-xr-x 1 moquist staff 8 Jul 12 2013 bhs.somedomain.com -> ssh-host 
lrwxr-xr-x 1 moquist staff 8 Jul 12 2013 devserver.somedomain.com -> mosh-host 

Of course I also have ~/.ssh/config set up, and my SSH keys are all in the appropriate ~/.ssh/authorized_keys files on remote systems.

But once all that's done, if I want to log in to a system, I can just type the name of the system (with tab completion). If I want to pipe something into or out of a command on a remote system (via ssh-host only), the system name just becomes another command:
james.mojo.home ~ $ aristotle "w | grep eviluser || echo eviluser is absent"
eviluser is absent
james.mojo.home ~ $ aristotle cat somefile | grep bits-i-want
### elided ###
james.mojo.home ~ $ for h in aristotle plato plantinga kant; do echo ====$h====; $h ls | grep lostfile; done
Obviously these are contrived examples, and there are plenty of other ways to do the same things. I've just found it convenient to think of hosts as commands, and this approach has let me do that.