def read_until(handle, start): # read each line until it has a certain start, and then puts # the start tag back while 1: pos = handle.tell() line = handle.readline() if not line: break if line.startswith(start): handle.seek(pos) return raise EOFError("%s tag cannot be found" % start)
def __execute_rpc__(device, rpc_command, timeout): rpc_command = '<?xml version="1.0" encoding="UTF-8"?><Request MajorVersion="1" MinorVersion="0">' \ + rpc_command + '</Request>' try: device.sendline(rpc_command) index = device.expect_exact(["</Response>", "ERROR: 0xa240fe00"], timeout=timeout) if index == 1: raise XMLCLIError('The XML document is not well-formed') except pexpect.TIMEOUT: raise TimeoutError("pexpect timeout error") except pexpect.EOF: raise EOFError("pexpect EOF error") # remove leading XML-agent prompt response_assembled = device.before + device.match response = re.sub('^[^<]*', '', response_assembled) root = ET.fromstring(response) if 'IteratorID' in root.attrib: raise IteratorIDError("Non supported IteratorID in Response object. \ Turn iteration off on your XML agent by configuring 'xml agent [tty | ssl] iteration off'. \ For more information refer to \ http://www.cisco.com/c/en/us/td/docs/ios_xr_sw/iosxr_r4-1/xml/programming/guide/xl41apidoc.pdf, \ 7-99.Turn iteration off on your XML agent.") childs = [x.tag for x in list(root)] result_summary = root.find('ResultSummary') if result_summary is not None and int(result_summary.get('ErrorCount', 0)) > 0: if 'CLI' in childs: error_msg = root.find('CLI').get('ErrorMsg') or '' elif 'Commit' in childs: error_msg = root.find('Commit').get('ErrorMsg') or '' else: error_msg = root.get('ErrorMsg') or '' error_msg += '\nOriginal call was: %s' % rpc_command raise XMLCLIError(error_msg) if 'CLI' in childs: cli_childs = [x.tag for x in list(root.find('CLI'))] if 'Configuration' in cli_childs: output = root.find('CLI').find('Configuration').text if output is None: output = '' elif 'Invalid input detected' in output: raise InvalidInputError('Invalid input entered:\n%s' % output) return root
def _read_until(handle, prefix): """Read each line until a prefix is found; then puts the prefix back.""" from exceptions import EOFError while True: pos = handle.tell() line = handle.readline() if not line: break if line.startswith(prefix): handle.seek(pos) return raise EOFError("'{}' prefix not found".format(prefix))
def open(self): """ Open a connection to an IOS-XR device. Connects to the device using SSH (pexpect) and drops into XML mode. """ device = pexpect.spawn('ssh -o ConnectTimeout={} -p {} {}@{}'.format( self.timeout, self.port, self.username, self.hostname), logfile=self.logfile) try: index = device.expect( ['\(yes\/no\)\?', 'password:'******'#', pexpect.EOF], timeout=self.timeout) if index == 0: device.sendline('yes') index = device.expect( ['\(yes\/no\)\?', 'password:'******'#', pexpect.EOF], timeout=self.timeout) if index == 1: device.sendline(self.password) elif index == 3: pass if index != 2: device.expect('#', timeout=self.timeout) device.sendline('xml') index = device.expect(['XML>', 'ERROR: 0x24319600'], timeout=self.timeout) if index == 1: raise XMLCLIError( 'XML TTY agent has not been started. Please configure \'xml agent tty\'.' ) except pexpect.TIMEOUT: raise TimeoutError("pexpect timeout error") except pexpect.EOF: raise EOFError("pexpect EOF error") self.device = device if self.lock_on_connect: self.lock()