python - Paramiko read and write with interactive remote client -
python - Paramiko read and write with interactive remote client -
i writing script connects cisco asr9k router using ssh, , executes xml command drops user shell prompt can issue xml requests , xml responses. able connect via ssh , execute command, have problem reading , writing interactive prompt.
session.py
import paramiko class session(object): """ create ssh session host using given username , password. keyword arguments: host -- ip of host want connect via ssh (default none) username -- username used in authentication host (default none) password -- password 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 , username , password: self.connect() self._stdin = none self._stdout = none self._stderr = none @property def username(self): homecoming self._username . . . def connect(self): """ connect host @ 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 write" else: self.stdin.write(message) #doesn't read xml response prompt def read(self): while not self.stdout.channel.exit_status_ready(): if self.stdout.channel.recv_ready(): info = self.stdout.channel.recv(1024) print "indside stdout" print info break
i create session object in thread class:
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): open('.'.join([self.threadname,'log']), "w+") 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 command drops 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 = [] xml_request in xml_requests: thread_list.append(xmlthread(xml_request)) thread in thread_list: thread.start() thread.join() #thread_list[0].start() if __name__ == '__main__': main()
the output doesn't include xml response, don't know why doesn't read whole buffer.
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>
any help appreciated, thanks.
sshclient.exec_command() used execute single command. sounds want drive interactive session done using sshclient.invoke_shell(). command executed returned xml response plus "\nxml>" prompt. you'll need extract response , throw prompt away when go interactive.
python xml ssh paramiko
Comments
Post a Comment