def _GetConfig(self, source): try: if source in ('running-config', 'startup-config'): result = self._Cmd('show %s' % source) else: raise exceptions.GetConfigError('source argument must be ' '"running-config" or ' '"startup-config".') if not result: return exceptions.EmptyConfigError('%s has an empty configuration.' % self.host) else: return result except exceptions.Error as e: raise exceptions.GetConfigError('Could not fetch config from %s. %s.' % (self.host, str(e)))
def _GetConfig(self, source_file): """Gets file or running configuration from the remote device. Args: source_file: A string, containing path to the file that should be retrieved from the remote device. It can also contain the defined reserved word self.CONFIG_RUNNING, in which case this method retrieves the running configuration from the remote device. Returns: response: A string, content of the retrieved file or running configuration. Raises: exceptions.GetConfigError: An error occured during the retrieval. exceptions.EmptyConfigError: Running configuration is empty. """ response = '' if source_file == self.CONFIG_RUNNING: try: response = self._Cmd('show configuration') except exceptions.CmdError: msg = ('Could not retrieve system configuration from %s' % repr(self.host)) logging.error(msg) raise exceptions.GetConfigError(msg) if not response: raise exceptions.EmptyConfigError( 'Configuration of %s is empty' % repr(self.host)) else: tempfile_ptr = tempfile.NamedTemporaryFile() try: self._GetFileViaSftp(local_filename=tempfile_ptr.name, remote_filename=source_file) except (paramiko.SFTPError, IOError) as e: msg = ('Could not retrieve configuration file %r from %s, ' 'error: %s' % (source_file, self.host, e)) logging.error(msg) raise exceptions.GetConfigError(msg) response = tempfile_ptr.read() return response