Create alias for ssh connecting
Use the intended way and write the options and aliases into ~/.ssh/config
:
Host 1
Port 12345
User my_user
HostName 123.123.123.1
Host 2
Port 12345
User my_user
HostName 123.123.123.2
and so on...
And then connect just using
ssh 1
ssh 2
...
This calls for a function -- simple and robust, whereas an alias
in this case would be fragile.
Something like this should do:
function ssht () {
[[ $1 =~ ^(1|2|3)$ ]] || { echo 'Not a valid last octet value !!' && return ;}
ip=123.123.123.$1
ssh my_user@"$ip" -p 12345
}
The condition [[ $1 =~ ^(1|2|3)$ ]]
makes sure you have entered one of 1, 2, 3 as first argument (any trailing argument is ignored).
Now, you can give the desired last octet as the first argument:
ssht 1
ssht 2
ssht 3
Put this in your ~/.bashrc
for having it available in any interactive session.
You can use patterns in ~/.ssh/config
. From man ssh_config
:
PATTERNS
A pattern consists of zero or more non-whitespace characters, ‘*’ (a
wildcard that matches zero or more characters), or ‘?’ (a wildcard that
matches exactly one character). For example, to specify a set of
declarations for any host in the “.co.uk” set of domains, the following
pattern could be used:
Host *.co.uk
The following pattern would match any host in the 192.168.0.[0-9] network
range:
Host 192.168.0.?
Combined with:
HostName
Specifies the real host name to log into. This can be used to
specify nicknames or abbreviations for hosts. If the hostname
contains the character sequence ‘%h’, then this will be replaced
with the host name specified on the command line (this is useful
for manipulating unqualified names). The character sequence ‘%%’
will be replaced by a single ‘%’ character, which may be used
when specifying IPv6 link-local addresses.
So, in your ~/.ssh/config
, put:
Host ?
Hostname 123.123.123.%h
Port 12345
User my_user
Then:
$ ssh -v 1
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /home/muru/.ssh/config
debug1: /home/muru/.ssh/config line 41: Applying options for ?
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 123.123.123.1 [123.123.123.1] port 12345.
debug1: connect to address 123.123.123.1 port 12345: Connection refused
ssh: connect to host 123.123.123.1 port 12345: Connection refused