While the previous ssh-to script works quite well, the following implementation is a bit cleaner and uses just the user's ~/.ssh/config file for reference. I got here because I have to log into many machines, and each may be on a different port and have a different username set up for me. I found the ssh config file, which is designed to deal with these issues, and then reasoned that it would be simpler and cooler if anything I added to the ssh config file automatically became an ssh-to without any additional work.
First, set up your ~/.ssh/config file. This file allows you to supply short names for your machines, as well as a different user, port number and other options for each target if you'd like. The format is very simple.
Host jschmoe
User jdoe
HostName JoeSchmoe.example.com
Port 4022
ForwardX11 yes
man ssh_config
for more information and a listing of available options. Having this file allows you to do jane@localhost:~ $ ssh jschmoe
and login to joeschmoe.example.com as user jdoe on port 4022.
Then, to take it one step further and because less typing is better, simply put the following lines in your ~/.bashrc file.
for name in `sed -n "/^Host/s/^Host //p" ~/.ssh/config`; do
alias $name="ssh $name"
done
This allows you to then issue
jane@localhost:~ $ jschmoe
to perform the above login.
See also:
- man ssh_config
- "Quick Logins with ssh Client Keys" (Linux Server Hacks #66)"
- "Turbo-mode ssh Logins" (Linux Server Hacks #67)"
- "Using ssh-Agent Effectively" (Linux Server Hacks #68)