Bulk upload of public keys to Linux servers

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.