Python 用于在“n”个实例之间进行无密码通信的结构脚本

Python 用于在“n”个实例之间进行无密码通信的结构脚本,python,ssh,fabric,ssh-keys,Python,Ssh,Fabric,Ssh Keys,以下代码用于在一台计算机上生成rsa密钥,并将公钥复制到另一台计算机上,以方便无密码通信 import pexpect def gen_rsa_key(): child = pexpect.spawn('ssh-keygen -t rsa') child.expect("Generating.*:") child.sendline("") i = child.expect([".*Overwrite (y/n)?", ".*:"]

以下代码用于在一台计算机上生成rsa密钥,并将公钥复制到另一台计算机上,以方便无密码通信

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命令在两台机器上运行它。 fab-f/root/ssh\u test.py-h9.114.192.36,9.114.192.40 gen\u rsa\u key copy\u rsa\u key

显示的输出很好。。但它无法在远程机器上的/root/.ssh/上生成rsa密钥。但是/root/.ssh/authorized_密钥已使用其他计算机的公钥更新


有人能帮我解决这个问题吗?请

除了执行python脚本之外,您似乎没有太多地使用fabric。请参见“确定”,但当您运行pexpect.spawn时,它将仅在本地运行。要在远程机器上运行东西,您必须使用fabric的run方法或其同类方法之一。例如,像runecho y | ssh keygen-t rsa-N\\