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

  • 相关阅读:
    php模式设计之 工厂模式
    SDK以及部署的SDK的思路
    手机用fiddler抓包开发测试
    搭建GIT服务端
    TP5.0以上数据库增加数据异常
    lnmp一键安装后的配置改动建议
    TPshop5最新版 安装 nginx 开启PATHINFO 模式资源加载路径加载失败问题,适用tp3.2PATHINFO模式REWRITE模式
    jquery写拉动条
    JS(JQ)分页 个人查看,没注释
    ecshop 分页
  • 原文地址:https://www.cnblogs.com/yooma/p/9872876.html
Copyright © 2020-2023  润新知