def sros_ssh_commander_prx(ip_address: ipaddress.IPv4Address, ip_address_prx: ipaddress.IPv4Address, username: str, username_prx: str, password: str, password_prx: str, commands: list, is_config_command: bool = True, stop_on_err: bool = True, stop_on_warn: bool = False, host_key_policy_enforce: object = 0) -> (int, list): """Description... TBD :type host_key_policy_enforce: int :type commands: list :type username: str :type username_prx: str :type ip_address: ipaddress.IPv4Address :type ip_address_prx: ipaddress.IPv4Address :type password: str :type password_prx: str :type is_config_command: bool :type stop_on_err: bool :type stop_on_warn: bool """ result = [] status = 0 ssh_client = paramiko.SSHClient() if host_key_policy_enforce == 0: ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) elif host_key_policy_enforce == 1: ssh_client.set_missing_host_key_policy(paramiko.RejectPolicy()) try: ssh_client.connect(ip_address_prx.__str__(), username=username_prx, password=password_prx, timeout=30.0) except socket.timeout as val_err: print('Connection timeout for IP@:', ip_address_prx.__str__(), val_err.__str__()) status = 1 return status, result ssh_client_transport = ssh_client.get_transport() channel_sh = ssh_client_transport.open_channel(kind='session') channel_sh.invoke_shell() time.sleep(5) # Catching greetings and setting env no more if channel_sh.recv_ready(): channel_sh.send('environment no more\n') time.sleep(5) channel_sh.in_buffer.empty() # Let's determine system name of proxy box channel_sh.send('show system information | match Name' + '\n') time.sleep(1) command_result = bytearray(channel_sh.in_buffer.empty()).decode('ascii') # Parsing result and determining is it ok or not try: system_name_prx = re.search('\n.*[ ]+:[ ]*([a-zA-Z0-9]+)[ ]*\r\n', command_result).group(1) except AttributeError as val_err: print('RegEx: Unable to determine system name of proxy box.', val_err) ssh_client.close() return 2, result channel_sh.send('ssh -l ' + username + ' ' + ip_address.__str__() + '\n') time.sleep(2) ssh_answer = bytearray(channel_sh.in_buffer.empty()).decode('ascii') if re.search('No route to destination', ssh_answer): print('No route to target box.') ssh_client.close() status = 3 return status, result if re.search('Are you sure you want to continue connecting \(yes/no\)\?', ssh_answer): channel_sh.send('yes\n') time.sleep(5) ssh_answer = bytearray(channel_sh.in_buffer.empty()).decode('ascii') if re.search(username + r'@' + ip_address.__str__() + "'s password:"******"'s password:"******"\nError: Invalid parameter\.", command_result) and stop_on_err: command_result = '!EXEC ERROR FOUND - STOP COMMAND EXECUTION! SROS output:\r\n' + command_result result.append((command, command_result)) print(80 * '=') print(command_result) print(80 * '=') return 7, result elif re.search( '\nINFO: .*\n\*(0,1)[AB]:' + system_name + '#[ ]+\r\n', command_result) and stop_on_warn: print('Attention! CLI warming:', command_result) result.append((command, command_result)) print(80 * '=') print(command_result) print(80 * '=') return 7, result channel_sh.send('admin save\n') # Parsing result and determining is it ok or not # CODE result.append((command, command_result)) number += 1 channel_sh.send('logout\n') ssh_client.close() return 0, result
def sros_show_ssh_commander( ip_address: ipaddress.IPv4Address, username: str, password: str, commands: List[str], debug: bool = False, speed_factor: int = 10, host_key_policy_enforce: int = 0) -> Union[List[Tuple], None]: """Description... :param speed_factor: :type host_key_policy_enforce: int :type commands: list :type username: str :type ip_address: ipaddress.IPv4Address :type password: str """ ssh_client = paramiko.SSHClient() if host_key_policy_enforce == 0: ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) elif host_key_policy_enforce == 1: ssh_client.set_missing_host_key_policy(paramiko.RejectPolicy()) try: ssh_client.connect(ip_address.__str__(), username=username, password=password, timeout=30.0) except socket.timeout as val_err: mod_log.warning('Connection timeout for IP@: %s ' '%s', ip_address.__str__(), val_err.__str__()) return None ssh_client_transport = ssh_client.get_transport() channel_sh = ssh_client_transport.open_channel(kind='session') try: channel_sh.invoke_shell() except paramiko.ssh_exception.SSHException as e: mod_log.warning( "Exception raised by failures in SSH2 protocol negotiation or logic errors." ) return None time.sleep(5) # Catching greetings if channel_sh.recv_ready(): channel_sh.send('environment no more\n') time.sleep(2) # TODO: add speed_factor here channel_sh.in_buffer.empty() else: ssh_client.close() mod_log.warning("Unable to get catch initial greetings.") return None # Iterating over show command list seq = 0 command_results = [] for command in commands: if seq % speed_factor == 0: time.sleep(5) channel_sh.in_buffer.empty() try: channel_sh.send(command + '\n') except socket.timeout as e: if debug: print('Socket timout exception!' ) # TODO: add node name when refactor to SROSNode return None # Waiting for output time.sleep(2) command_output = channel_sh.in_buffer.empty() command_results.append( (command, bytearray(command_output).decode('ascii'))) seq += 1 ssh_client.close() return command_results