【问题标题】:Paramiko read and write with interactive remote clientParamiko 使用交互式远程客户端读写
【发布时间】:2014-06-19 01:49:19
【问题描述】:

我正在编写一个脚本,该脚本使用 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

我在线程类中创建了一个会话对象:

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> 

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: python xml ssh paramiko


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多