Score:0

How can I use regex for hostname in ssh_config?

sz flag

So, my local address keeps on changing on my Lan. Can't change the dhcp settings. Addresses are all like: 192.168.A.x, 192.168.A.y, 192.168.A.z. When I switch off the dhcp server, next time it gives 192.168.B.x, 192.168.B.y, 192.168.B.z. It keeps x, y and z constant.

So, i have tried to edit ~/. ssh/config like this

Host pc1
           HostName pc.local
           User xx
           Port 22

Host pc2
           HostName "192.168.???.n1"
           User xx
           Port 22

Host pc3
           HostName "192.168.*.n2"
           User xx
           Port 22

Calling ssh pc1 works fine. Also using ssh 192.168.A.x with the full adress works. But, calling ssh pc2 gives the error could not resolve hostname 192.168.???.n1

jp flag
Patterns can be used with `Host` but not in `Hostname`.
Score:3
in flag

No, you can't. Especially not in this case. How is the ssh client supposed to "guess" what IP you want to connect to?

Set up a proper DHCP server or configure static IPs on your servers.

sz flag
Got some instructions from here: https://serverfault.com/questions/803902/how-can-i-match-a-cidr-range-for-an-ssh-config-host-entry/803906
Archemar avatar
mx flag
wildcard will work on `Host` line, e.g. `host 192.168.*.125<cr>user xx` would allow selecting user `xx` when using `ssh 192.168.33.125`
sz flag
@Archemar, how would I then access that via ssh command?? Do I use `Host 192.168.*.n1 pc2` then `ssh pc2`??
Archemar avatar
mx flag
by using `ssh 192.168.33.125` ssh can't do "retro aliasing" that DNS's work.
Score:1
ve flag

it's not possible to use regex for hostname in ssh_config, but you can use the local domain name as you had done with PC1 thus

Host pc1
           HostName pc.local
           User xx
           Port 22

Host pc2
           HostName pc2.local
           User xx
           Port 22

Host pc3
           HostName pc3.local
           User xx
           Port 22

using the hostname you would never need to know the IP. Then you can ssh to the hostname

ssh pc.local
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.