def read_nonblocking(self, size=1, timeout=None): """Read, handling terminal control input from an HP ProCurve. This may or may not actually block, as per its parent. Args: size: An int, the minimum size block to return. timeout: An optional float, wait only timeout seconds at most. Returns: A string, the filtered output. """ start = time.time() if timeout == -1: timeout = self.timeout while True: if timeout and time.time() > start + timeout: return '' in_data = SshSpawn.read_nonblocking(self, size=size, timeout=timeout) logging.vlog(4, 'Unfiltered: %r', in_data) if in_data and self._read_nonblocking_buf: logging.debug('Prepending data: %r', self._read_nonblocking_buf) in_data = self._read_nonblocking_buf + in_data self._read_nonblocking_buf = '' filtered = self._Filter(in_data) escape_location = filtered.find('\x1B') if escape_location != -1: logging.debug('Partial ANSI tag in filtered data: %r', filtered) self._read_nonblocking_buf = filtered[escape_location:] filtered = filtered[:escape_location] if filtered: return filtered
if pindex == 2: raise exceptions.SetConfigError( 'Could not copy temporary ' 'file to startup-config.') except (pexpect.EOF, pexpect.TIMEOUT), e: raise exceptions.SetConfigError(str(e)) elif destination_file == self.CONFIG_RUNNING: try: # This is not working, unfortunately. Cannot copy a file to a running # config, raised support case RFE2901 self._Cmd('copy slot1 running-config %s' % file_name) except exceptions.CmdError, e: raise exceptions.SetConfigError(str(e)) # We need to 'write memory' if we are doing running-config. logging.vlog( 3, 'Attempting to copy running-config to startup-config ' 'on %s(%s)', self.host, self.loopback_ipv4) try: self._Cmd('wr mem') except exceptions.CmdError, e: raise exceptions.SetConfigError( 'Failed to write startup-config ' 'for %s(%s). Error was: %s' % (self.host, self.loopback_ipv4, str(e))) finally: # Now remove the remote temporary file. # If this fails, we may have already copied the file, so log warnings # regarding this and return this information to the user in the # RPC response, so that they can delete the files. if destination_file in self.NON_FILE_DESTINATIONS:
def _MaybeFindPrompt(self): """Perform real login and then enable if we have an enable password.""" # We always run this for HP, no matter the state of self._find_prompt. self._prompt = r'(?:^|\n|\r)([A-Za-z0-9\._-]+)(?:>|#) ' # Shake out the prompt. We may be facing a Password prompt or # a 'Press any key to continue' prompt. self.child.send('\r') # Only send the password once. password_sent = False try: # Login. while True: logging.vlog(3, 'Expecting prompt %r', self._prompt) compiled_regexes = self.child.compile_pattern_list( [self._prompt, r'Press any key to continue', 'Password:'******'Invalid password', 'Unable to verify password']) i = self.child.expect(compiled_regexes, timeout=10) if i == 0: re_str = (re.escape(self.child.match.group(1)) + r'(?:>|#) ') logging.vlog(3, 'Prompt set to %r', re_str) self.re_prompt = re.compile(re_str) break elif i == 1: logging.vlog(3, 'Pressing any key (space)') self.child.send(' ') elif i == 2 and not password_sent: # Send the password only once. try: self.child.sendline(self._password) logging.vlog(3, 'Sent user password (again) to %r', self._host) password_sent = True except (pexpect.TIMEOUT, pexpect.EOF) as e: self._ssh_client = None raise ConnectionError(str(e)) elif i <= 3 and i < 5: logging.error('CONNECT_ERROR Incorrect user password on %r', self._host) # Sleep momentarily before expecting again to break buffer swap races. time.sleep(0.05) # Enable. password_sent = False logging.vlog(3, 'Enabling for HP on %r', self._host) self.child.sendline('enable') while True: i = self.child.expect([self._prompt, 'Password:'******'Invalid password', 'Unable to verify password'], timeout=10) if i == 0: # Found the prompt, we're enabled. break elif i == 1 and not password_sent: if self._enable_password is not None: self.child.sendline(self._enable_password) logging.vlog(3, 'Sent enable password to %r', self._host) else: self.child.sendline(self._password) logging.vlog(3, 'Sent user password to %r', self._host) password_sent = True elif i <= 3 and i < 5: logging.error('CONNECT_ERROR Incorrect user password on %r', self._host) # Sleep momentarily before expecting again to break buffer swap races. time.sleep(0.05) except (pexpect.TIMEOUT, pexpect.EOF) as e: self._ssh_client = None raise ConnectionError(str(e))
def _Filter(self, text): text = re.sub(self.NEWLINE_RE, '\n', text) text = re.sub(self.ANSI_RE, '', text) logging.vlog(4, 'Filtered: %r', text) return text
["Total bytes", self._connection.re_prompt, "Error"], timeout=self.timeout_act_user ) if pindex == 2: raise exceptions.SetConfigError("Could not copy temporary " "file to startup-config.") except (pexpect.EOF, pexpect.TIMEOUT), e: raise exceptions.SetConfigError(str(e)) elif destination_file == self.CONFIG_RUNNING: try: # This is not working, unfortunately. Cannot copy a file to a running # config, raised support case RFE2901 self._Cmd("copy slot1 running-config %s" % file_name) except exceptions.CmdError, e: raise exceptions.SetConfigError(str(e)) # We need to 'write memory' if we are doing running-config. logging.vlog( 3, "Attempting to copy running-config to startup-config " "on %s(%s)", self.host, self.loopback_ipv4 ) try: self._Cmd("wr mem") except exceptions.CmdError, e: raise exceptions.SetConfigError( "Failed to write startup-config " "for %s(%s). Error was: %s" % (self.host, self.loopback_ipv4, str(e)) ) finally: # Now remove the remote temporary file. # If this fails, we may have already copied the file, so log warnings # regarding this and return this information to the user in the # RPC response, so that they can delete the files. if destination_file in self.NON_FILE_DESTINATIONS:
['Total bytes', self._connection.re_prompt, 'Error'], timeout=self.timeout_act_user) if pindex == 2: raise exceptions.SetConfigError('Could not copy temporary ' 'file to startup-config.') except (pexpect.EOF, pexpect.TIMEOUT), e: raise exceptions.SetConfigError(str(e)) elif destination_file == self.CONFIG_RUNNING: try: # This is not working, unfortunately. Cannot copy a file to a running # config, raised support case RFE2901 self._Cmd('copy slot1 running-config %s' % file_name) except exceptions.CmdError, e: raise exceptions.SetConfigError(str(e)) # We need to 'write memory' if we are doing running-config. logging.vlog(3, 'Attempting to copy running-config to startup-config ' 'on %s(%s)', self.host, self.loopback_ipv4) try: self._Cmd('wr mem') except exceptions.CmdError, e: raise exceptions.SetConfigError('Failed to write startup-config ' 'for %s(%s). Error was: %s' % (self.host, self.loopback_ipv4, str(e))) finally: # Now remove the remote temporary file. # If this fails, we may have already copied the file, so log warnings # regarding this and return this information to the user in the # RPC response, so that they can delete the files. if destination_file in self.NON_FILE_DESTINATIONS: try: