Bulk upload SSH public key
The vicissitudes of the first world
First you need to install Python
Yum Install Python-y
Write a Python script like this:
#vi ssh.py
#!/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) # #如果公钥不是在默认位置的话, change the Ssh-copy-id to ssh-copy-id-i public key location.
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 Pexpect. TIMEOUT:
Print >>sys.stderr, ' timeout '
Except Pexpect. Eof:
Print P.before
Print >>sys.stderr, ' <the end> '
(Single use method: use: Python ssh.py 192.168.2.10 password1 #用户名和密码没错一般都可以. )
2: Python combined with shell for bulk upload
The IP and password of the server are listed in a TXT file, for example (a.txt):
192.168.1.1 Password1
192.168.1.2 Password2
192.168.1.3 Password3
Then call the following with the shell script
[Email protected] opt]# VI ssh.sh
Cat A.txt |while Read line; Do
Ip= ' echo $line | awk ' {print '} '
Passwd= ' echo $line | awk ' {print $} '
Python ssh.py $ip $passwd
Done
Finally executed./ssh.sh
If there is an error
is because the Pexpect module that does not have Python installed is installed as follows:
Pexpect Module Installation
Pexpect belongs to a third party, so it needs to be installed,
The current version is 3.3 https://pypi.python.org/pypi/pexpect/
Installation steps:
TAR-XZVF pexpect-3.3.tar.gz
CD pexpect-3.3
Python setup install (require root)
However, this installation requires root privileges, and if there is no root authority, can it be used?
The answer is yes, you just need to put the path of Lib into Sys.path. This allows you to use the Pexpect
#!/usr/bin/env python
Import Sys
Sys.path.append (' Pexpect-3.3/build/lib ')
Confirm Installation Success:
>>> Import Pexpect
>>> dir (pexpect)
[' EOF ', ' exceptionpexpect ', ' PY3 ', ' TIMEOUT ', ' __all__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package_ _ ', ' __path__ ', ' __revision__ ', ' __version__ ', ' _run ', ' codecs ', ' errno ', ' fcntl ', ' is_executable_file ', ' OS ', ' Pty ', ' re ', ' Resource ', ' Run ', ' Runu ', ' searcher_re ', ' searcher_string ', ' select ', ' signal ', ' spawn ', ' spawnu ', ' split_command_ ' Line ', ' stat ', ' struct ', ' sys ', ' termios ', ' time ', ' traceback ', ' tty ', ' types ', ' which ']
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 "One World ups and downs" blog, please be sure to keep this source http://yishifuchen.blog.51cto.com/10542999/1676776
Bulk upload SSH public key