Millet Porridge

English version of https://corvo.myseu.cn

0%

OpenSSH Series 7: OpenSSH with Wildcards

As you may have used bash or other shells, there is a powerful tool called wildcard which allows you to perform actions on more than one file at a time. When you want to list all files in the directory start with foo, you can use this:

1
2
ls foo*
foo1 foo2 foo3

Consider that you have a bulk of machines like this(just fake hosts):

机器名 IP 端口 用户名 密钥
gg-android01-node.xx.yy.zz 48.132.144.01 22000 corvo ~/.ssh/id_rsa_test
gg-android02-node.xx.yy.zz 48.132.144.02 22000 corvo ~/.ssh/id_rsa_test
gg-android03-node.xx.yy.zz 48.132.144.03 22000 corvo ~/.ssh/id_rsa_test
gg-android04-node.xx.yy.zz 48.132.144.04 22000 corvo ~/.ssh/id_rsa_test
ms-vscode01-master.xx.yy.zz 42.188.144.01 22000 corvo ~/.ssh/id_rsa_test
ms-vscode02-master.xx.yy.zz 42.188.144.02 22000 corvo ~/.ssh/id_rsa_test
ms-vscode03-master.xx.yy.zz 42.188.144.03 22000 corvo ~/.ssh/id_rsa_test
fb-react01-teer.xx.yy.zz 59.143.138.01 22000 corvo ~/.ssh/id_rsa_test
fb-react02-teer.xx.yy.zz 59.143.138.02 22000 corvo ~/.ssh/id_rsa_test
fb-react03-teer.xx.yy.zz 59.143.138.03 22000 corvo ~/.ssh/id_rsa_test

When you want to put them in ssh_config, it seems that you have to write the configuration this way, using a separate configuration for each host.

1
2
3
4
5
6
7
8
9
10
11
12
13
Host android01
User corvo
HostName gg-android01-node.xx.yy.zz
Port 22000
IdentityFile ~/.ssh/id_rsa_test

Host android02
User corvo
HostName gg-android02-node.xx.yy.zz
Port 22000
IdentityFile ~/.ssh/id_rsa_test

...

It’s not an elegant way, This configuration is full of duplicates of the same user and the same port: Here is a beautiful style:

1
2
3
4
Host gg-* ms-* fb-*
User corvo
Port 22000
IdentityFile ~/.ssh/id_rsa_test

Now you can log in to your host directly with ssh.

1
~ ❤  ssh fb-react03-teer.xx.yy.zz