Batch upload public keys to Linux servers

Source: Internet
Author: User

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:

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.