Fabric-script使密码减少'n'实例之间的通信

时间:2015-02-19 12:07:50

标签: python ssh fabric ssh-keys

以下是在一台计算机上生成rsa-key并将公钥复制到另一台计算机以便于密码密码通信的代码。

import pexpect

def gen_rsa_key():
        child = pexpect.spawn('ssh-keygen -t rsa')
        child.expect("Generating.*:")
        child.sendline("")
        i = child.expect([".*Overwrite (y/n)?", ".*:"])
        if i == 0:
                print i
                child.sendline("y")
                child.expect(".*:")
                child.sendline("")
                child.expect(".*:")
                child.sendline("")
                print child.read()
        else:
                child.sendline("")
                child.expect(".*:")
                child.sendline("")
                print child.read()

        child.close()

def copy_rsa_key():
        servers = ["9.114.192.36", "9.114.192.40"]
        for ip in servers:
                cmd = "ssh-copy-id %s" %ip
                child = pexpect.spawn(cmd)
                i = child.expect(["[Pp]assword:","Are you sure you want to continue connecting (yes/no)?",".* WARNING: .*."])
                if i == 0:
                        child.sendline("passw0rd")
                        print child.read()
                if i == 1:
                        child.sendline("yes")
                        child.expect("[Pp]assword:")
                        child.sendline("passw0rd")
                        print child.read()
                else:
                        print "All keys were skipped because they already exist on the remote system."
        child.close()
        print i

我使用以下fab命令在2台机器上运行它。 fab -f /root/ssh_test.py -H 9.114.192.36,9.114.192.40 gen_rsa_key copy_rsa_key

显示的输出很好..但它无法在/root/.ssh/的远程机器上生成rsa-keys。但是/root/.ssh/authorised_keys用其他机器的公钥更新了。

任何人都可以帮我解决这个问题。请

0 个答案:

没有答案