python 处理 SSH [许多客户端]
我管理 3 个服务器 [ Linux ],我必须每 6 小时打开这些服务器上的脚本
,因此登录每个服务器都需要一点时间..所以我
import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SPAM=1
while SPAM==3:
ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
SPAM+=1
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
SPAM+=1
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
ssh.close()
SPAM+=1
很好地编写了这段代码,但它也不起作用;(我想输入每一项并运行脚本并转到 另一台服务器没有关闭连接或终止脚本,所以请帮助我。
现在编辑后,
我编辑它,我遇到了新问题
,我将其添加到我的代码中:
import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for F1 in ZI1:
ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
stdin, stdout, stderr = ssh.exec_command('uname -a')
print stdout.readlines()
ssh.close()
for F2 in ZI2:
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
ssh.exec_command('ls -la')
print stdout.readlines()
ssh.close()
for F3 in ZI3:
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
ssh.exec_command('pwd')
print stdout.readlines()
ssh.close()
我给每个不同的命令来查看它是否有效,我得到了非常奇怪的输出
['Linux xxxx 2.6.18-028stab070.14 #1 SMP 11 月 18 日星期四 16:04:02 MSK 2010 x 86_64 x86_64 x86_64 GNU/Linux\n'] ['Linux xxxx 2.6.18-028stab070.14 #1 SMP 11 月 18 日星期四 16:04:02 MSK 2010 x 86_64 x86_64 x86_64 GNU/Linux\n'] [] [] [] []
我说第一个服务器执行命令uname -a
,第二个服务器执行命令ls -la
,但它给它们提供了相同的命令,第三个服务器没有不执行任何操作
i mange 3 server [ Linux ] and i have to turn on scripts on these servers every 6 hours
so it's take a lite bit time to login in each one .. so i made this code
import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SPAM=1
while SPAM==3:
ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
SPAM+=1
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
SPAM+=1
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
print stdout.readlines()
ssh.close()
SPAM+=1
well it's not working as well ;( i wana to enter each one and run the script and go to
another server without closing the connection or terminating the script so please help me.
after edit
now i edit it and i got new issues
i add this to my code :
import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for F1 in ZI1:
ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
stdin, stdout, stderr = ssh.exec_command('uname -a')
print stdout.readlines()
ssh.close()
for F2 in ZI2:
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
ssh.exec_command('ls -la')
print stdout.readlines()
ssh.close()
for F3 in ZI3:
ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
ssh.exec_command('pwd')
print stdout.readlines()
ssh.close()
and i give each one different command to see does it work and i got very stranger output
['Linux xxxx 2.6.18-028stab070.14 #1 SMP Thu Nov 18 16:04:02 MSK 2010 x
86_64 x86_64 x86_64 GNU/Linux\n']
['Linux xxxx 2.6.18-028stab070.14 #1 SMP Thu Nov 18 16:04:02 MSK 2010 x
86_64 x86_64 x86_64 GNU/Linux\n']
[]
[]
[]
[]
i said first server do the command uname -a
and second one do the command ls -la
but it's give them together the same command third server doesn't execute anything
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想在这里做什么炸猴子?因为是重复某事,但你不想重复任何事。
您看到这两行代码之间的区别了吗:
您的第二个和第三个服务器仍然使用第一个服务器的标准输出。您需要再次拥有 stdin、stdout、stderr。
What the fried monkey are you trying to do here? For is repeating something, but you don't want to repeat anything.
Do you see the difference between these two lines of code:
Your second and third servers are both still using the stdout from the first server. You need to have the stdin, stdout, stderr again.
您可能应该使用 Fabric 来执行此操作。这对你来说会简单很多。
You probably should be using Fabric to do this. It would be a lot simpler for you.
看来您的问题是您正在调用
stdout.readlines()
,它将继续读取 SSH 命令的输出,直到没有更多输入可供读取......所以您的在那之前程序不会进展。作为一个简单的解决方案,您可以在自己的线程中执行每个命令,也可以fork
并在自己的进程空间中执行它。It appears that your problem is that you're calling
stdout.readlines()
, which is going to continue to read the output from the SSH command until there is no more input to read... so your program doesn't advance until then. As a simple solution, you can execute each command in its own thread, or you canfork
and execute it in its own process space.恕我直言,您正在从错误的一端解决这个问题...
而不是从远程主机建立连接,为什么不更好地设置 cron 远程计算机上的脚本。
IMHO, you are approaching this problem from the wrong end...
Instead of making a connection from remote host, why don't you better set up cron scripts on the remote machines.