Hostnames as Bash Commands

Smylers Smylers at stripey.com
Fri Jan 4 16:29:33 GMT 2008


Since I mentioned this in the pub last night I thought I'd post the code
-- it's my way of configuring Bash (on Ubuntu Linux) so that typing a
hostname as a command will SSH to that server.

It uses the recent Debian/Ubuntu Bash command_not_found_handle extension
(so likely won't work on other OSes).  By default this kicks in if you
try to run a program which is available in the OS but not installed,
telling you which package you need to install to get it:

  $ echo znetnerg gungpure | rot13
  The program 'rot13' is currently not installed. You can install it by
  typing: sudo apt-get install bsdgames
  bash: rot13: command not found

So I made it try that first and if it fails use the _known_hosts
function defined by extended Bash completion (so you need that as well)
to check if the not-found command is a known hostname.

The code seems quite long for what it's doing, but most of it is just
dealing with (or commenting on) various bits of hatefulness:

function command_not_found_handle
# checks for unrecognized commands being known hostnames, SSHing to them if so
# (after the normal Debian check for being an installable command)
{

  # First do the normal Debian thing of seeing if the command is available in a
  # package which isn't installed, and if so tell the user what to type to
  # install it (but only if this support is installed):
  local installable_checker=/usr/lib/command-not-found
  if [[ -x $installable_checker ]]
  then

    # If this does find a command then we want to stop looking here.
    # Unfortunately the checker returns the same exit code either way, so the
    # only way of telling is to capture the output to a variable:
    local installable_msg
    installable_msg=$($installable_checker -- "$1" 2>&1)

    # If find something then we want to propagate the exit code, so capture
    # that too; note this is why installable_msg above is declared and set on
    # separate lines, otherwise we end up recording the success of the local
    # command rather than that of the subshell:
    local installable_exit_code=$?

    # If we got anything then display it and we're done:
    if [[ $installable_msg ]]
    then
      echo $installable_msg
      return $installable_exit_code
    fi
  fi

  # Otherwise see if the command is a known host (but only if the _known_hosts
  # function, defined for Bash completion, is available):
  if declare -F _known_hosts > /dev/null
  then

    # As it's a completion function _known_hosts has an awkward interface -- we
    # have to set up a fake command-line for it to 'complete':
    COMP_WORDS="$1"
    COMP_CWORD=0
    _known_hosts

    # If its first suggested 'completion' is what we supplied then it was a
    # hostname, so try SSHing to it:
    if [[ "${COMPREPLY[0]}" == "$1" ]]
    then
      if ssh $1
      then

        # bash(1) says that we should return 0 if the command is now available
        # for future use; well, it isn't, but we have successfully SSHed and
        # doing this same thing again will work, so let's count that as good
        # enough, so as to suppress the message about the command not being
        # found:
        return
      fi
      # Note that if the SSH fails its exit code gets lost, because the only
      # permitted failure code we can return is that below.

    fi
  fi

  # We're still here, so indicate nothing's been found:
  return 127
}

Smylers


More information about the london.pm mailing list