Open mosheni24 opened 1 year ago
@mosheni24 Can you include your code?
Also can you try setting this attribute to True and see if it makes a difference?
netmiko_conn.ansi_escape_codes = True
from netmiko import import time import json from constants import import re import logging from datetime import datetime
logging.basicConfig(filename='netmiko_global_recent.log', level=logging.DEBUG) logger = logging.getLogger("netmiko")
def ict_desktop(deviceIp,command,customer_name,product_type,transaction_Id,device_vendor,fileDir,plat,operation_type,device_type_value): print("Inside ICT desktop script") fh = open(fileDir+""+"ictDesktop_properties.json") print("opened json file successfully") ict_data = json.loads(fh.read()) print("Json data loaded successfully") for key,value in ict_data.items(): if key == customer_name: for val1 , val2 in value.items(): customer_username = str(value.get("customer_username")) customer_password = str(value.get("customer_password")) customer_prim_desktop = str(value.get("customer_prim_desktop")) customer_sec_desktop = str(value.get("customer_sec_desktop")) cust_device_username = str(value.get("cust_device_username")) cust_device_password = str(value.get("cust_device_password")) cust_device_secret = str(value.get("cust_device_secret"))
primary_response = primary_connection(customer_prim_desktop,customer_username,customer_password,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id)
print("Primary Response",primary_response)
if('status' in primary_response and primary_response['status'] == FAILED and 'errorCode' in primary_response and primary_response['errorCode'] == PRIM_ICT_DESK_ERR):
secondary_response = secondary_connection(customer_sec_desktop,customer_username,customer_password,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id)
print("Checking Secondary response")
if any(item in primary_response for item in SEC_ICT_DESKTOP_FAILURE):
print("Failed connecting to ICT Desktops")
return "Failed connecting to ICT Desktop"
return secondary_response
return primary_response
def primary_connection(customer_prim_desktop,customer_username,customer_password,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id): print("Inside primary connection") primary_connection = { 'device_type': 'autodetect', 'ip': customer_prim_desktop, 'username': customer_username, 'password': customer_password, 'secret': cust_device_secret, 'session_log': '/software/bea/python/source/netmiko_session_recent.log', 'ssh_strict' : False, 'port': 22, 'session_timeout': 180, 'timeout': 200, 'auth_timeout': 120, 'blocking_timeout': 60, 'banner_timeout': 180, 'global_delay_factor': 6 } try: net_connect = ConnectHandler(**primary_connection)
except Exception as e:
print(str(e))
return final_response("",deviceIp,str(e),FAILED,device_type_value,PRIM_ICT_DESK_ERR,transaction_Id)
else:
print("Success")
connection_output = ict_device_connection(net_connect,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id)
return connection_output
def secondary_connection(customer_sec_desktop,customer_username,customer_password,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id): print("Inside secondary connection") secondary_connection = { 'device_type': 'autodetect', 'ip': customer_sec_desktop, 'username': customer_username, 'password': customer_password, 'secret': cust_device_secret, 'session_log': '/software/bea/python/source/netmiko_session_recent.log', 'ssh_strict' : False, 'port': 22, 'session_timeout': 180, 'timeout': 200, 'auth_timeout': 120, 'blocking_timeout': 60, 'banner_timeout': 180, 'global_delay_factor': 6 } try: print("Before Netconnect in Secondary") net_connect = ConnectHandler(**secondary_connection) time.sleep(1) print("After Netconnect in Secondary")
except Exception as e:
error = str(e)
return final_response("",deviceIp,str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
else:
print("Success")
connection_output = ict_device_connection(net_connect,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id)
return connection_output
def ict_device_connection(net_connect,cust_device_username,cust_device_password,cust_device_secret,deviceIp,command,operation_type,device_type_value,transaction_Id): try: ict_desktop__prompt = net_connect.find_prompt() net_connect.write_channel("ssh "+cust_device_username+"@"+ deviceIp + "\n") max_loops = 30 i = 1 output_two = None while i <= max_loops: output_two = net_connect.read_channel() if any(item in output_two for item in CONNECT_DEVICE_FAILURE) or any(item in output_two for item in CONNECT_DEVICE_PROMPT): break else: time.sleep(1) i=i+1 if any(item in output_two for item in CONNECT_DEVICE_FAILURE): print("Failed While Login To Device") error_msg = "Failed While Login To Device" return final_response(output_two,deviceIp,error_msg,FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
elif re.search("Unable to negotiate with",output_two):
start_index = output_two.find("offer: ")
print("INDEX " , start_index)
after_value = output_two[start_index:]
print("After value " , after_value)
strip_value = after_value.strip("offer: ")
print("Splitted Value " , strip_value)
split_value = strip_value.split(',')
print("Cipher Value list " , split_value)
for item in split_value :
first_cipher_value = split_value[0]
print("First cipher value ", first_cipher_value)
net_connect.write_channel("ssh -c "+first_cipher_value+" " +cust_device_username+"@"+ deviceIp + "\n")
time.sleep(3)
output_three = net_connect.read_until_prompt()
time.sleep(.5)
print("Output Three " , output_three)
if re.search("Are you sure you want to continue connecting",output_three):
net_connect.write_channel("yes\n")
time.sleep(1)
net_connect.write_channel(cust_device_password + "\n")
time.sleep(3)
net_connect.write_channel("\n")
elif re.search("Are you sure you want to continue connecting",output_two):
net_connect.write_channel("yes\n")
time.sleep(3)
net_connect.write_channel(cust_device_password + "\n")
time.sleep(3)
net_connect.write_channel("\n")
elif 'password' in output_two or 'Password' in output_two:
net_connect.write_channel(cust_device_password + "\n")
time.sleep(3)
#net_connect.write_channel("\n")
print("After device password ", net_connect.find_prompt())
else:
print("Exception Occured While Login To Device")
error_msg = "Exception Occured While Login To Device"
return final_response("",deviceIp,error_msg,FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
device_prompt = net_connect.find_prompt()
if(ict_desktop__prompt != device_prompt):
redispatch(net_connect, device_type = 'generic_termserver')
print("After redispatch")
print("check value of device type ",device_type_value)
if device_type_value == 'hp_comware':
logging.info("Transaction ID" + transaction_Id)
print("Entering enable mode on HP device")
net_connect.write_channel("su" + "\n")
net_connect.write_channel(cust_device_secret)
net_connect.write_channel("\n")
print("Entered device secret for HP device")
time.sleep(3)
net_connect.write_channel("\n")
print("enable prompt {}::>\n".format(net_connect.find_prompt()))
terminal_length = net_connect.write_channel("screen-length disable")
net_connect.write_channel("\n")
net_connect.write_channel("\n")
if operation_type == "config":
try:
command = command.replace("system-view\n", "")
print("Executing config commands for HP device")
config_terminal = net_connect.write_channel("system-view")
print("config promp HP {}:::\n".format(net_connect.find_prompt()))
print("Config command HP", command)
config_cmd_output = net_connect.send_config_set(command,exit_config_mode=False)
print("config prompt HP after {}:::".format(net_connect.find_prompt()))
print("Config command output for device " + device_type_value)
net_connect.disconnect()
if re.search("%",config_cmd_output):
return final_response(config_cmd_output,deviceIp,"Error Occured While Executing Configurations Commands On Device: "+str(e),FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
else:
return final_response(config_cmd_output,deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
except Exception as e:
return final_response("",deviceIp,"Exception Occured While Executing Configurations Commands On Device: "+str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
elif operation_type == "read":
try:
**net_connect.ansi_escape_codes = True**
device_prompt = net_connect.find_prompt()
#start_time = datetime.now()
print("Executing read commands on HP device")
net_connect.clear_buffer()
read_cmd_output = net_connect.send_command(command,expect_string=device_prompt)
logging.info("Device Op" + read_cmd_output)
#end_time = datetime.now()
#print("Total time: {}".format(end_time - start_time))
print("Read command output for HP device" , read_cmd_output)
net_connect.write_channel("\n")
net_connect.write_channel("exit\n")
net_connect.disconnect()
print("Returning Output For HP Commmmmmmmmmmmmmm");
if any(item in read_cmd_output for item in DOESNT_EXIST):
return final_response("",deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
elif re.search("%",read_cmd_output):
return final_response("",deviceIp,"Invalid Show Command",FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
elif not read_cmd_output:
return final_response("",deviceIp,"No Output Received",FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
return final_response(read_cmd_output,deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
except Exception as e:
print("Inside Exception Block",str(e))
return final_response("",deviceIp,"Exception Occured While Executing Show Command On Device: "+str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
elif device_type_value == 'juniper_junos':
print("Inside Juniper part")
terminal_length = net_connect.write_channel("set cli screen-length 0")
net_connect.write_channel("\n")
#time.sleep(2)
print("Juniper device terminal length set to 0")
if operation_type == "config":
try:
print("Executing config commands for Juniper device")
config_cmd_output = net_connect.send_config_set(command,exit_config_mode=False)
net_connect.disconnect()
if re.search("syntax error",config_cmd_output):
return final_response(config_cmd_output,deviceIp,"Error Occured While Executing Configurations Commands On Device: "+str(e),FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
else:
return final_response(config_cmd_output,deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
except Exception as e:
return final_response("",deviceIp,"Exception Occured While Executing Configurations Commands On Device: "+str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
elif operation_type == "read":
try:
print("Executing read commands for Juniper device")
net_connect.ansi_escape_codes = True
device_prompt = net_connect.find_prompt()
read_cmd_output = net_connect.send_command(command,expect_string=device_prompt,strip_prompt=True,strip_command=True)
net_connect.disconnect()
if any(item in read_cmd_output for item in DOESNT_EXIST) or re.search("vlan with tag .*\d does not exist", read_cmd_output):
return final_response("",deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
elif re.search("unknown command",read_cmd_output):
return final_response("",deviceIp,"Invalid Show Command",FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
elif not read_cmd_output:
return final_response("",deviceIp,"No Output Received",FAILED,device_type_value,DEVICE_ERROR,transaction_Id)
return final_response(read_cmd_output,deviceIp,"",SUCCESS,device_type_value,"",transaction_Id)
except Exception as e:
return final_response("",deviceIp,"Exception Occured While Executing Show Command On Device: "+str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
elif device_type_value == 'cisco':
terminal_length = net_connect.write_channel("terminal length 0")
if operation_type == "config":
print("Inside cisco config part")
elif operation_type == "read":
print("Inside cisco read part")
read_cmd_output = net_connect.send_command(command)
return ""
else:
return final_response("",deviceIp,"Unable To Login To Device",FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
except Exception as e:
return final_response("",deviceIp,"Exception Occured While Processing The Request: "+str(e),FAILED,device_type_value,FUNCTNL_ERROR,transaction_Id)
def final_response(output,deviceIp,failure_message,status,device_type_value,errorCode,transaction_Id): final_response = {} print("Inside Final Response") if status == "Success": final_response["deviceOutput"] = output final_response["deviceIP"] = deviceIp final_response["status"] = status if status == "Success": final_response["description"] = "Command executed successfully" elif status == "Failed": final_response["description"] = failure_message if status == "Failed": final_response["executionDetails"] = output final_response["transactionId"] = transaction_Id final_response["deviceType"] = device_type_value if status == "Failed": final_response["errorCode"] = errorCode print("Exiting Final Response") print("Final response ",final_response) return final_response
@ktbyers yes i am alraedy using the below to escape unicode characters net_connect.ansi_escape_codes = True
Am trying to connect to the device via jumphost
I am connecting to a HPE Conware device via a Jump Host. My connections work fine. But the output retrieved by netmiko has duplicate entries
Have attached the output via script as well as when i do it manually on device.
Display Vlan Brief Output_Manual.txt display_vlan_brief_op_script.txt