FQDN SSH login function

I don’t use unqualified hostnames for ssh logins. They’re too dependent on local context. The command ssh myhost leaves it up to the local DNS resolver to append a domain name to myhost, and too often the local DNS resolver is influenced by a DHCP server of unknown provenance.

On the other hand, laziness dictates that I reduce the amount of typing I do to login, so the command ssh myhost.mysubdomain.mydomain isn’t a winner for me either.

The solution is a domain-specific login function I’ve added to my .bash_profile shell initialization file:

function mli {
  ssh ${2:+"${2}@"}${1:?"usage: $FUNCNAME host [user]"}.madboa.com
}

Notes:

  • The general usage is mli host [user]. The host parameter gets prepended to my domain (madboa.com). If there’s a user parameter, it gets prepended to the host’s FQDN with an @ character between the username and hostname.
  • mli stands for “madboa login”; I have functions with similar three-letter names for other domains.
  • ${2:+"${2}@"} means “if there’s a second parameter passed to this function, expand it to “parameter@"; it’s an optional username.
  • ${1:?"usage: $FUNCNAME host [user]"} means “expand the first parameter passed to this function here; if there is no parameter, exit function with the error message found after the question mark.”

Logging into my web server is as simple as

# this expands to "ssh www.madboa.com"
mli www

If I want to login as root, I just add a user parameter:

# this expands to "ssh root@www.madboa.com"
mli www root

Getting a usage message is as simple as invoking the function without any parameters.

[~]$ mli
-bash: 1: usage: mli host [user]