简体   繁体   English

由于脚本执行时间,Paramiko 块中的命令

[英]Commands in Paramiko block due to script execution time

hello guys i have three server and i mange it from SSH so i made this script to run my Register script "Register.py" so everyday i turn on Register mode so the problem how i can login to multiple SSH connection without close the other大家好,我有三台服务器,我从 SSH 管理它,所以我制作了这个脚本来运行我的注册脚本“Register.py”,所以我每天都打开注册模式,所以问题是我如何在不关闭另一个的情况下登录多个 SSH 连接

import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"Administrator"}
ZI2={"ip":"192.168.1.3","pass":"AdminTeachers"}
ZI3={"ip":"192.168.1.4","pass":"AdminStudents"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for F1 in ZI1:
    ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
    ssh.exec_command('./register.py -time 6') #6 hour so the script still working for 6 hours
    ssh.close()
for F2 in ZI2:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('./register.py -time 6')
    ssh.close()
for F3 in ZI3:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('./register.py -time 6')
    ssh.close()

so what i have to do to open 3 sessions without stopping script !!所以我必须做什么才能在不停止脚本的情况下打开 3 个会话!

I'd suggest looking at Fabric .我建议看看Fabric It may help you with working with SSH connections.它可以帮助您使用 SSH 连接。

The way you are currently doing it blocks beacuse you are not logging out of the hosts for six hours.您当前执行此操作的方式会阻塞,因为您没有在六个小时内退出主机。

Multiprocessing:多处理:

If you need to see return codes from the script, you should open your connections to each host using python's multiprocessing module .如果您需要查看脚本的返回码,您应该使用python 的 multiprocessing 模块打开与每个主机的连接。

nohup: nohup:

Another method (that will not allow you to see the script's return value via paramiko ) is to use nohup to disassociate the script from the shell.另一种方法(不允许您通过paramiko查看脚本的返回值)是使用nohup将脚本与 shell 解除关联。 That will put it in the background and allow you to logout.这会将其置于后台并允许您注销。 To do this use...要做到这一点,使用...

    ssh.exec_command('nohup ./register.py -time 6 &') 

Typos:错别字:

BTW, you had typos in the last loop... ZI2 should be ZI3 in the last loop... furthermore, the for -loops are unnecessary... I fixed your very last iteration... Acks to @johnsyweb for spotting more of the OP's typos than I did...顺便说一句,您在最后一个循环中有错别字... ZI2在最后一个循环中应该是ZI3 ... 此外, for循环是不必要的...我修复了您的最后一次迭代...感谢@johnsyweb 发现更多OP的错别字比我做的...

ssh.connect(ZI3["ip"],username='root', password=ZI3["pass"])
ssh.exec_command('./register.py -time 6')   # <------------- missing s in ssh
ssh.close()

Another way is using Thread if you need do some action based on return of Register.py如果您需要根据 Register.py 的返回执行一些操作,另一种方法是使用 Thread

See example:参见示例:

import paramiko
import os
import sys
from threading import Thread

SERVER_LIST = [{"ip":"192.168.1.2","pass":"Administrator"},{"ip":"192.168.1.4","pass":"AdminStudents"},{"ip":"192.168.1.3","pass":"AdminTeachers"}]



class ExecuteRegister(Thread):
    def __init__ (self,options):
        Thread.__init__(self)
        self.options = options       
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())



    def run(self):
        try:
           self.ssh.connect(self.options['ip'],username='root', password=self.options["pass"])
           self.ssh.exec_command('./register.py -time 6') #6 hour so the script still working for 6 hours
           self.ssh.close()
        except:
           print sys.exc_info()



for server in SERVER_LIST:
    Register = ExecuteRegister(server)
    Register.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM