Python 使用交互式远程客户端进行Paramiko读写

Python 使用交互式远程客户端进行Paramiko读写,python,xml,ssh,paramiko,Python,Xml,Ssh,Paramiko,我正在编写一个脚本,它使用SSH连接到Cisco ASR9K路由器,然后它执行xml命令,将用户放入一个类似于shell的提示符中,在那里您可以发出xml请求并返回xml响应。我可以通过SSH连接并执行命令,但在交互式提示符下读写时遇到问题 session.py import paramiko class Session(object): """ Create a ssh session with a host using a given username and password.

我正在编写一个脚本,它使用SSH连接到Cisco ASR9K路由器,然后它执行xml命令,将用户放入一个类似于shell的提示符中,在那里您可以发出xml请求并返回xml响应。我可以通过SSH连接并执行命令,但在交互式提示符下读写时遇到问题

session.py

import paramiko

class Session(object):
    """ Create a ssh session with a host using a given username and password.

    Keyword Arguments:
    host -- IP of the host you want to connect to via ssh (default None)
    username -- Username to be used in authentication with the host (default None)
    password -- Password to be used in authentication (default None)
    """

    def __init__(self, host=None, username=None, password=None):
        self._host = host
        self._username = username
        self._password = password
        self._session = None
        if host and username and password:
            self.connect()

        self._stdin = None
        self._stdout = None
        self._stderr = None

    @property
    def username(self):
        return self._username

    .
    .
    .

    def connect(self):
        """ Connect to the host at the IP address specified."""
        self.session = paramiko.SSHClient()
        self.session.load_system_host_keys()
        self.session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.session.connect(self.host, username=self.username, password=self.password, allow_agent=False, 
                             look_for_keys=False)

    def close(self):
        self.session.close()

    def execute_command(self, command):
        self.stdin, self.stdout, self.stderr = self.session.exec_command(command)

    def write(self, message):
        print message
        if self.stdin == None:
            print "error with write"
        else:
            self.stdin.write(message)
    #Doesn't read the xml response from the prompt
    def read(self):
        while not self.stdout.channel.exit_status_ready():
            if self.stdout.channel.recv_ready():
                data = self.stdout.channel.recv(1024)
                print "Indside stdout"
                print data
                break
我在thread类中创建会话对象:

class xmlThread(threading.Thread):
    def __init__(self, xml_tuple):
        threading.Thread.__init__(self)
        self.xml_request = xml_tuple[0]
        self.threadname = '-'.join([xml_tuple[1],'thread'])


    def log(self, message):
        with open('.'.join([self.threadname,'log']), "w+") as fp:
            fp.write(message)

    def run(self):
        ssh_session = Session(host='10.17.6.111', username='lab', password='lab')
        ssh_session.execute_command('xml echo format') #Run the command that drops into the xml prompt
        ssh_session.read()
        ssh_session.write(self.xml_request)
        ssh_session.read()
        ssh_session.close()



def main():
    xml_requests = generate_xml_requests()
    thread_list = []
    for xml_request in xml_requests:
        thread_list.append(xmlThread(xml_request))

    for thread in thread_list:
        thread.start()
        thread.join()

    #thread_list[0].start()

if __name__ == '__main__':
    main()
我得到的输出不包括xml响应,我不知道为什么它不读取整个缓冲区

Output

Indside stdout



<?xml version="1.0" encoding="UTF-8"?>
                    <Request MajorVersion="1" MinorVersion="0">
                        <Get>
                           <Configuration Source="CurrentConfig">
                                 <AAA>
                                 </AAA>
                          </Configuration>
                        </Get>
                    </Request>
Indside stdout
XML> 
输出
内舷舷梯
内舷舷梯
XML>
非常感谢您的帮助。

SSHClient.exec_command()用于执行单个命令。听起来您想驱动一个交互式会话,该会话是使用SSHClient.invoke_shell()完成的。您执行的命令返回了一个xml响应和一个“\nXML>”提示。当您进行交互时,您需要提取响应并扔掉提示