expand hostnames to fqdn before calling ssh?

I make use of an .ssh/config file to set my username appropriately based on a given portion of a subdomain, e.g.

Host *widgetshop.com*
   User foobar
   ControlMaster auto
   ControlPath ~/.ssh/socket-%r@%h:%p

Host *
   ControlMaster auto
   ControlPath ~/.ssh/socket-%r@%h:%p

This works if I write:

ssh foo.widgetshop.com

but does not work if I were to write:

ssh foo

which upon search path resolution becomes the same fqdn.

Anyone have a way to expand shortnames before calling ssh, or an alternative approach?


Perhaps you can use the host command:

ssh $(host -t A  foo | cut -f1 -d" ")

Put that into a shell script (replace "foo" with "$1") and exec ssh.


As of OpenSSH 6.5, you can do this with the CanonicalizeHostname option in ssh_config. Here's an example that should do what you want:

CanonicalizeHostname yes
CanonicalDomains widgetshop.com

Host *.widgetshop.com
     User foobar