简体   繁体   中英

BASH label for IP addresses

My fault: I have been so busy learning other linux stuff that I completely neglected the power of bash.

I have a number of systems to access remotely for very simple operations. The problem is that I need to remember each single IP address. And they are a lot.

Using aliases in ~./bashrc is an option:

 alias ssh_customer1='ssh root@10.X.X.X'
 alias ssh_customer2='ssh root@10.X.X.Y'
 alias copy_customer1='scp * root@10.X.X.X:/etc/example/'
 alias copy_customer2='scp * root@10.X.X.Y:/etc/example/'
 alias get_customer1='scp root@10.X.X.X:/etc/example/* .'
 alias get_customer2='scp root@10.X.X.Y:/etc/example/* .'

but the flexibility is minimal.

Another possibility is to define functions using the name of system as a parameter but I don't like this:

sshx('customer1')
scpx('customer2')

I would prefer to just replace a label with the corresponding IP address without the need to remember it, and use standard commands:

ssh root@ip_customer1
scp root@ip_customer2:/etc/example/* .

Is this possible?

Setup a ~/.ssh/config file:

$ cat ~/.ssh/config
Host cust1
    HostName 10.X.X.X
    User root
Host cust2
    HostName 10.X.X.Y
    User root

Now you can use:

ssh cust1

Another cool thing is that you can now attach identity files to each server:

$ cat ~/.ssh/config
Host cust1
    HostName 10.X.X.X
    User root
    IdentityFile ~/.ssh/cust1.id_rsa
Host cust2
    HostName 10.X.X.Y
    User root
    IdentityFile ~/.ssh/cust2.id_rsa

This will let you use ssh and scp without password, assuming the key is without password or ssh-agent is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM