Python Forum
ssh to multi server and keep seesion to working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ssh to multi server and keep seesion to working
#1
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 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to take the tar backup files form remote server to local server sivareddy 0 1,917 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Opening CSV file from SFTP server not working cluelessintern 0 2,780 Apr-08-2020, 08:10 PM
Last Post: cluelessintern
  Multi-client server Saga 2 5,014 Jul-18-2017, 02:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020