开发者

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

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":"AdminStud开发者_如何学Goents"}
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 !!


I'd suggest looking at Fabric. It may help you with working with SSH connections.


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.

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. 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...

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

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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜