def connect(self, host, username, password, port=None, re_string='', look_for_keys=False): """ Connect to device through ssh :param re_string: regular expration of end of output :return: str """ ExpectSession.init(self, host, username, password, port) s = "Host: {0}, port: {1}, username: {2}, password: {3}, timeout: {4}".format(self._host, self._port, self._username, self._password, self._timeout) if self._logger: self._logger.info(s) # with open(r'c:\temp\ssh_connect.txt', 'a') as f: # f.write(s + '\n') self._handler.connect(self._host, self._port, self._username, self._password, timeout=self._timeout, banner_timeout=30, allow_agent=False, look_for_keys=look_for_keys) self._current_channel = self._handler.invoke_shell() self._current_channel.settimeout(self._timeout) output = self.hardware_expect(re_string=re_string, timeout=self._timeout) if self._logger: self._logger.info(output) return output
def __init__(self, *args, **kwargs): ExpectSession.__init__(self, self._init_handler(), *args, **kwargs) self._buffer_size = TCPSession._DEFAULT_BUFFER if 'buffer_size' in kwargs: self._buffer_size = kwargs['buffer_size'] if self._port is not None: self._port = int(self._port)
def __init__(self, *args, **kwargs): ExpectSession.__init__(self, self._init_handler(), *args, **kwargs) # self._handler.load_system_host_keys() # self._handler.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # # self._current_channel = None # self._buffer_size = SSHSession._DEFAULT_BUFFER if 'buffer_size' in kwargs: self._buffer_size = kwargs['buffer_size']
def connect(self, host, username, password, port=None, re_string=''): """ Connect to device :param expected_str: regular expression string :return: """ ExpectSession.init(self, host, username, password, port) server_address = (self._host, self._port) self._handler = self._init_handler() self._handler.connect(server_address) self._handler.settimeout(self._timeout) output = self.hardware_expect(re_string=re_string) return output
def login(self, address, username, password, command_logger=None): self.log('login') self.log('address=' + str(address)) self.log('username='******'password='******'port=' + str(self._port)) self.log('prompt=' + str(self._prompt)) self.log('Connecting...') try: self._session.connect(address, username, password, port=self._port, re_string=self._prompt) except: self.log('Retrying') from common.cli.expect_session import ExpectSession ExpectSession.init(self._session, address, username, password, self._port) self._session._handler.connect(address, self._port, username, password, timeout=self._session._timeout, banner_timeout=30, allow_agent=False, look_for_keys=True) self._session._current_channel = self._session._handler.invoke_shell() self._session._current_channel.settimeout(self._session._timeout) self.log(self._session.hardware_expect(re_string=self._prompt, timeout=self._session._timeout)) self.log('Connected')
def connect(self, host, username, password, port=None, re_string=''): """ Connect to device :param expected_str: regular expression string :return: """ ExpectSession.init(host, username, password, port) self._handler.open(self._host, int(self._port), self._timeout) if self._handler.get_socket() is None: raise Exception('TelnetSession', "Can't connect to device!") expect_map = OrderedDict() expect_map['[Ll]ogin:|[Uu]ser:'] = lambda: self.send_line(self._username) expect_map['[Pp]assword:'] = lambda: self.send_line(self._password) output = self.hardware_expect(re_string=re_string, expect_map=expect_map) self._logger.info(output) return output
def connect(self, host, username, password, port=None, re_string=''): """ Connect to device through ssh :param re_string: regular expration of end of output :return: str """ ExpectSession.init(host, username, password, port) self._logger.info("Host: {0}, port: {1}, username: {2}, password: {3}, timeout: {4}". format(self._host, self._port, self._username, self._password, self._timeout)) self._handler.connect(self._host, self._port, self._username, self._password, timeout=self._timeout, banner_timeout=30, allow_agent=False, look_for_keys=False) self._current_channel = self._handler.invoke_shell() self._current_channel.settimeout(self._timeout) output = self.hardware_expect(re_string=re_string, timeout=self._timeout) self._logger.info(output) return output
def __init__(self, *args, **kwargs): ExpectSession.__init__(self, self._init_handler(), *args, **kwargs)