Python Paramiko并行执行到远程unix主机
我有一个下面的脚本,我作为一个cetralized用户使用它在远程主机上执行命令,但是这个脚本会读取主机文件并逐个执行命令,但是它也会保留在会话中,直到它没有与shell解除链接,因此,我希望有一个并行执行,即在运行脚本时,它应该能够分叉多个ssh连接并登录到主机,并在命令执行后立即退出 请让我知道,如果你们有任何技巧或专家意见。虽然我使用paramiko作为这些hosta-rae-legarcy UNIX主机,但由于一些限制,我无法使用ansible或类似的实用程序Python Paramiko并行执行到远程unix主机,python,Python,我有一个下面的脚本,我作为一个cetralized用户使用它在远程主机上执行命令,但是这个脚本会读取主机文件并逐个执行命令,但是它也会保留在会话中,直到它没有与shell解除链接,因此,我希望有一个并行执行,即在运行脚本时,它应该能够分叉多个ssh连接并登录到主机,并在命令执行后立即退出 请让我知道,如果你们有任何技巧或专家意见。虽然我使用paramiko作为这些hosta-rae-legarcy UNIX主机,但由于一些限制,我无法使用ansible或类似的实用程序 import parami
import paramiko
with open('/data/CR9432/SunOS.txt', 'r') as f:
for host in f:
remote_host = host.rstrip()
remote_pass = "pass123"
smart_user = "mtrooper"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote_host, username=smart_user, password=remote_pass)
transport = ssh.get_transport()
session = transport.open_session()
session.set_combine_stderr(True)
session.get_pty()
#for testing purposes we want to force sudo to always to ask for password. because of that we use "-k" key
############################################
#session.exec_command("shutdown -y -i5 -g0")
############################################
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
#you have to check if you really need to send password here
stdin.write(remote_pass +'\n')
stdin.flush()
print"\n"
print "------------------------------------------------------------------------------"
print "Command Execution Output On Hostname: "
print "------------------------------------------------------------------------------"
for line in stdout.read().splitlines():
print 'host: %s: %s' % (remote_host, line)
看看这篇文章,它告诉我们如何与paramiko同时创建ssh连接@postoronnim,这看起来很好,但是我不理解代码上的并行执行系统。是的,它只创建多个ssh客户端,一次一个。我认为可以使用python线程模块将这些客户机映射到多个线程上,以并行地完成工作。这是一个例子@postoronnim,我已经看过了。