• Python paramiko ssh 在同一个session里run多个命令


    import threading, paramiko
    
    strdata=''
    fulldata=''
    
    class ssh:
        shell = None
        client = None
        transport = None
    
        def __init__(self, address, username, password):
            print("Connecting to server on ip", str(address) + ".")
            self.client = paramiko.client.SSHClient()
            self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
            self.client.connect(address, username=username, password=password, look_for_keys=False)
            self.transport = paramiko.Transport((address, 22))
            self.transport.connect(username=username, password=password)
    
            thread = threading.Thread(target=self.process)
            thread.daemon = True
            thread.start()
    
        def close_connection(self):
            if(self.client != None):
                self.client.close()
                self.transport.close()
    
        def open_shell(self):
            self.shell = self.client.invoke_shell()
    
        def send_shell(self, command):
            if(self.shell):
                self.shell.send(command + "
    ")
            else:
                print("Shell not opened.")
    
        def process(self):
            global strdata, fulldata
            while True:
                # Print data when available
                if self.shell is not None and self.shell.recv_ready():
                    alldata = self.shell.recv(1024)
                    while self.shell.recv_ready():
                        alldata += self.shell.recv(1024)
                    strdata = strdata + str(alldata)
                    fulldata = fulldata + str(alldata)
                    strdata = self.print_lines(strdata) # print all received data except last line
    
        def print_lines(self, data):
            last_line = data
            if '
    ' in data:
                lines = data.splitlines()
                for i in range(0, len(lines)-1):
                    print(lines[i])
                last_line = lines[len(lines) - 1]
                if data.endswith('
    '):
                    print(last_line)
                    last_line = ''
            return last_line
    
    
    sshUsername = "SSH USERNAME"
    sshPassword = "SSH PASSWORD"
    sshServer = "SSH SERVER ADDRESS"
    
    
    connection = ssh(sshServer, sshUsername, sshPassword)
    connection.open_shell()
    connection.send_shell('cmd1')
    connection.send_shell('cmd2')
    connection.send_shell('cmd3')
    time.sleep(10)
    print(strdata)    # print the last line of received data
    print('==========================')
    print(fulldata)   # This contains the complete data received.
    print('==========================')
    connection.close_connection()

  • 相关阅读:
    IOS微信浏览器返回事件popstate监听
    图解用HTML5的popstate如何玩转浏览器历史记录
    如何在深层嵌套ngRepeat中获取不同层级的$index
    angular ng-repeat 如何实现嵌套
    AJAX的工作原理
    利用angular指令监听ng-repeat渲染完成后执行脚本
    AngularJS中$http服务的简单用法
    AngularJS的Filter用法详解
    angularJS发起$http.post请求后台收不到数据解决方案
    AngularJs的UI组件ui-Bootstrap分享(十四)——Carousel
  • 原文地址:https://www.cnblogs.com/yooma/p/9872876.html
Copyright © 2020-2023  润新知