def execute_command(self, command, is_read_first_page=False): """ execute command :param is_read_first_page: :param command: :return: """ if self.connection is None: save_log('IP:%s,SSH登录失败,不能执行命令' % self.host_ip, 'ERROR') raise Exception(self.host_ip + 'SSH登录失败,不能执行命令') time.sleep(0.5) self.channel.send(command + '\r\n') exe_result = self.__fetch_execute_result(self.channel) self.channel.send(' ') more_result = self.__fetch_execute_result(self.channel) exe_result = exe_result + more_result last_res = '' if not is_read_first_page: while True: if not self.check_is_output_finish(exe_result): exe_result = exe_result + '\n' self.channel.send(' ') more_result = self.__fetch_execute_result(self.channel) if more_result == last_res: break last_res = more_result exe_result = exe_result + more_result else: break exe_result = self.__exe_space_one_more_time(exe_result) return exe_result
def execute_command(self, command, is_read_first_page=False): """ execute command :param is_read_first_page: :param command: :return: """ if self.is_connection is False: save_log('telnet登录失败,不能执行命令') return None self.tn.write(command.encode('ascii') + b'\r\n') command_result = self.__fetch_execute_result() all_result = command_result if not is_read_first_page: # only read first page while True: self.tn.write(' '.encode('ascii') + b'\r\n') command_result = self.__fetch_execute_result() filter_command = command_result.replace('\r\n', '').replace(' ', '') all_result = all_result + command_result # print(command_result) if len(filter_command) == 0 or filter_command[-1] in '>#': break all_result = self.__exe_space_one_more_time(all_result) # save_log(all_result) return all_result
def __create_connection(self): """ create telnet connection :return: """ try: self.tn = telnetlib.Telnet() self.tn.open(self.host_ip, port=23) time.sleep(0.2) self.tn.read_until(':'.encode('ascii'), timeout=2) time.sleep(0.2) self.tn.write(self.user_name.encode('ascii') + b'\n') time.sleep(0.2) self.tn.read_until(":".encode('ascii'), timeout=2) time.sleep(0.2) self.tn.write(self.password.encode('ascii') + b'\n') time.sleep(0.2) is_login_success = self.__check_is_login_success() if is_login_success: save_log(content='telnet 登录%s 成功' % self.host_ip, level='INFO') return is_login_success return False except: save_log(content='telnet 登录%s失败' % self.host_ip, level='ERROR') return False
def init_connection(self): """ init connection :return: """ try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) save_log('尝试登录SSH,IP:%s' % self.host_ip, 'INFO') ssh.connect(self.host_ip, 22, self.user_name, self.password, allow_agent=False, look_for_keys=False, timeout=5) if ssh is not None: save_log('SSH登录IP:%s成功' % self.host_ip, 'INFO') return ssh save_log('SSH登录IP:%s失败' % self.host_ip, 'ERROR') return None except Exception as e: print(e) save_log('SSH登录IP:%s失败' % self.host_ip, 'ERROR') return None
def __init__(self, host_ip=None, user_name=None, password=None, device_info=None): """ init method :param host_ip: :param user_name: :param password: :param device_info: """ DeviceConnectionInterface.__init__(self, host_ip=host_ip, user_name=user_name, password=password, device_info=device_info) save_log(content='尝试telnet登陆:%s' % self.host_ip, level='INFO') self.is_connection = self.__create_connection() self.connection = self.tn
def __create_connection(self): """ create telnet connection :return: """ try: self.tn = telnetlib.Telnet() self.tn.open(self.host_ip, port=23) self.tn.read_until( self.command_template.login_username_end_print.encode('ascii'), timeout=2) self.tn.write(self.user_name.encode('ascii') + b'\n') self.tn.read_until( self.command_template.login_password_end_print.encode('ascii'), timeout=2) self.tn.write(self.password.encode('ascii') + b'\n') is_login_success = self.__check_is_login_success() return is_login_success except: save_log(content='telnet 登录%s失败' % self.host_ip, level='ERROR') return False
def __check_is_login_success(self): """ check is login success :return: """ command_result = self.__input_blank_space().strip() if len(command_result) > 0 and (command_result[-1] in '#>'): save_log(content='telnet 登录%s 成功' % self.host_ip, level='INFO') return True else: command_result = self.__input_blank_space().strip() if len(command_result) > 0 and (command_result[-1] in '#>'): save_log(content='telnet 登录%s 成功' % self.host_ip, level='INFO') return True save_log(content='telnet 登录%s 失败' % self.host_ip, level='ERROR') return False
def _create_connection(self): """ create telnet connection :return: """ try: self.tn = telnetlib.Telnet() self.tn.open(self.host_ip, port=23) _ = self.read_all_content() _ = self.read_all_content() self.tn.write(self.user_name.encode('ascii') + b'\n') _ = self.read_all_content() _ = self.read_all_content() self.tn.write(self.password.encode('ascii') + b'\n') _ = self.read_all_content() _ = self.read_all_content() self.is_connected = self.__check_is_login_success() if self.is_connected: save_log(content='telnet 登录%s成功' % self.host_ip, level='INFO') else: save_log(content='telnet 登录%s失败' % self.host_ip, level='ERROR') except Exception as e: print(e) save_log(content='telnet 登录%s失败' % self.host_ip, level='ERROR')
def close(self): self.tn.close() save_log('关闭%s连接' % self.host_ip, 'INFO')
def close(self): if self.is_connection: self.connection.close() save_log('关闭%s连接' % self.host_ip, 'INFO')