How to use python to bulk distribute native ssh public keys?

when we use the ansible tool, the default is to use ssh for remote control.
you need to generate key pairs in a way similar to ssh-keygen-t rsa
, and then use the ssh-copy-id command to distribute the public key file to a remote host
and enter the password of the remote host once, so that you can use ansible to remotely control the host

.

so how do you do this with python? The main purpose of
is to accomplish this task:

batch distribute public keys, record failed hosts, delete key information on remote hosts, etc.?


ansible can complete the task you said.
if you want to use more python code to control the flow, you can use another automated deployment tool, fabric


-sharp!/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 pexpect.TIMEOUT:
    print >>sys.stderr, 'timeout'
except pexpect.EOF:
    print p.before
    print >>sys.stderr, '<the end>'

problem solved

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e7bad8-87c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e7bad8-87c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?