All along, through the Ssh-copy-id in the manual upload public key, think if there are thousands of servers need to upload public key, this method is OK. Try to implement the next batch upload public key to the server, introduce me to try a method.
1: First try a machine from Python script, Python scripts are 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.expect (expect_list) print p.before + expect_list[idx], if idx = = 0: print "Yes" p.sendline (' yes ') elif idx == 1: print password P.sendline (password) except&nbsP;pexpect. timeout: print >>sys.stderr, ' Timeout ' except pexpect. eof: print p.before print >>sys.stderr, ' <the end> '
How to use: Python sendpublic.py 192.168.2.10 password #用户名和密码没错一般都可以.
2: Python combined with shell for bulk upload
The IP and password of the server are listed 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 for implementing bulk uploads is as follows:
Cat A.txt |while Read Line;do ip= ' echo $line | awk ' {print $ ' #提取ip password= ' echo $line | awk ' {print $} ' #提取password python sendpublic.py $ip $passworddone
As noted above, it is important to note:
A.txt format, the first parameter is IP, the second is the password. The middle is separated by a space, generally use this on the bulk upload public key to the server is no problem.
If the public key is not in the default location, the statement in the Python script, p = pexpect.spawn (' ssh-copy-id%s '% IP), changes the Ssh-copy-id to ssh-copy-id-i public key location.
In addition, the above program before running to be a public key and password ssh-keygen-t RSA (two-time return)
This article is from the "Zhangweihong" blog, make sure to keep this source http://zhuangweihong.blog.51cto.com/8808431/1669134
Bulk upload of public keys to Linux servers