Issue
I have a list of about 50 systems with unique ip addresses that i need to regularly ssh into. I know these systems by a four digit alphanumeric number, but am constantly having to look up the ip addresses whenever I ssh into them. I would like to create a bash script that contains a list of all of the IP addresses for each machine, and i can type:
ssh_script aa11
and it executes:
ssh 111.222.333.444
Follow up question: some of the systems require a port # but i would still like to just type ssh_script bb22 and it does
ssh 333.444.555.666 -p 4444
Is this possible?
Solution
This should be easy as such.
#!/bin/sh
port=22
case $1 in
aa11) host=111.222.333.444;;
bb22) host=333.444.555.666
port=4444;;
*) echo "$0: host $1 unknown" >&2
exit 2;;
esac
exec ssh -p "$port" "$host"
(This script uses no Bash features, so I put #!/bin/sh
. It should work with Bash as well, of course.)
But probably a better solution is to configure this in your ~/.ssh/config
instead; then it will also work transparently for scp
and programs which invoke ssh
internally, like for example git
.
Host aa11
Hostname 111.222.333.444
Host bb22
Hostname 333.444.555.666
Port 4444
See the ssh_config manual page for details about this file.
You can combine the two;
#!/bin/sh
test -e ~/.ssh/config_too || cat <<\: >~/.ssh/config_too
Host aa11
Hostname 111.222.333.444
Host bb22
Hostname 333.444.555.666
Port 4444
:
exec ssh -F ~/.ssh/config_too "$@"
(Not all sh
instances necessarily support ~
even though it's now mandated by POSIX; maybe switch to "$HOME"/.ssh/config_too
if you have an oldish sh
on some systems.)
Answered By - tripleee