Batch upload public keys to Linux servers
For a long time, ssh-copy-id has been used to manually upload the public key. This method is enough if thousands of servers need to upload the public key. I tried to upload the public key to the Linux server in batches and introduced a method.
1: First try a machine through the python script. The python script is as follows:
#! /Usr/bin/python
Import sys
Import pexpect
Ip = sys. argv [1]
Password = sys. argv [2]
Expect_list = ['(yes/no)', 'password: ']
P = pexpect. spawn ('ssh-copy-id % s' % ip)
Try:
While True:
Idx = p. CT (expect_list)
Print p. before + expect_list [idx],
If idx = 0:
Print "yes"
P. sendline ('yes ')
Elif idx = 1:
Print password
P. sendline (password)
Failed t pexpect. TIMEOUT:
Print> sys. stderr, 'timeout'
Failed t pexpect. EOF:
Print p. before
Print> sys. stderr, '<the end>'
Usage: python sendpublic. py 192.168.2.10 password # the user name and password are correct.
2: The above python combined with shell for batch upload
Put the Server IP address and password in a TXT file, for example, (B .txt ):
192.168.1.1 password1
192.168.1.2 password2
192.168.1.3 password3
The shell program used for batch upload is as follows:
Cat a.txt | while read line; do
Ip = 'echo $ line | awk' {print $1} '# extract ip
Password = 'echo $ line | awk' {print $2} ''# extract password
Python sendpublic. py $ ip $ password
Done
Note:
A.txt format. The first parameter is ip address, and the second parameter is password. Use spaces to separate them. Generally, it is okay to upload the public key to the server in batches.
If the public key is not in the default position, the statement p = pexpect In the python script. spawn ('ssh-copy-id % s' % ip), change the ssh-copy-id to the ssh-copy-id-I Public Key location.
In addition, the above program requires a public key and password ssh-keygen-t rsa before running (Press ENTER twice)
This article permanently updates the link address: