Python Forum

Full Version: ssh to multi server and keep seesion to working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I use paramiko to implementation of SSH.


I want use paramiko and prevent do SSH job againlike that flow:

Quote:SSH to servers >> keep all SSH sessions >> do job 1 on server A >> do job 2 on server B >> End all sessions when finish jobs


But I stuck on this so current my flow:

Quote:SSH to server A >> do job 1 >> SSH to server B >> do job 2 >>SSH to servers >> do job 3 >> End

I use this SSH class

import threading, paramiko
 
class ssh:
    shell = None
    client = None
    transport = None
 
    def __init__(self, address, username):
        print("Connecting to server on ip", str(address) + ".")
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        k = paramiko.RSAKey.from_private_key_file('id_rsa')
		self.client.connect(hosts, username=username, pkey =k)
        self.client.connect(address, username=username,)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username)
 
        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()
 
    def closeConnection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()
 
    def openShell(self):
        self.shell = self.client.invoke_shell()
 
    def sendShell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("Shell not opened.")
 
    def process(self):
        global connection
        while True:
            # Print data when available
            if self.shell != None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = str(alldata, "utf8")
                strdata.replace('\r', '')
                print(strdata, end = "")
                if(strdata.endswith("$ ")):
                    print("\n$ ", end = "")
So anyone have experience of this case ?