def write_descr(ip, username, password, enable_secret, localint, descr): ssh_connection = ConnectHandler( device_type = 'cisco_ios', ip = ip, username = username, password = password, secret = enable_secret ) ssh_connection.enable() rt = ssh_connection.find_prompt() + "\n" rt = ssh_connection.send_command("conf t", delay_factor=0) rt = ssh_connection.send_command("int "+localint, delay_factor=0) rt = ssh_connection.send_command("description "+descr, delay_factor=0) ssh_connection.disconnect()
def main(): for device in rtr_list: rtr_conn = ConnectHandler(**device) prompt = rtr_conn.find_prompt() rtr_conn.config_mode() output = rtr_conn.send_config_from_file(config_file='4_8_cfg.txt') print output
def show_version(current_device): device_ip = socket.gethostbyname(current_device) command = "show version" net_connect = ConnectHandler( device_type=device_type, ip=device_ip, username=current_operator.username, password=current_operator.password ) net_connect.find_prompt() net_connect.enable() output = net_connect.send_command(command) return (output)
def main(): ip_addr = '50.76.53.27' port_rtr1 = 22 port_rtr2 = 8022 username = '******' password = '******' #password = getpass() pynet1 = { 'device_type': 'cisco_ios', 'ip': ip_addr, 'username': username, 'password': password, 'port': port_rtr1, } pynet_rtr1 = ConnectHandler(**pynet1) pynet_rtr1.find_prompt() print "we are getting into config mode" pynet_rtr1.config_mode() if pynet_rtr1.check_config_mode(): print "yes, we are in config mode" else: print "sometthing is wrong, we are not in config mode!"
def setup_module(module): module.EXPECTED_RESPONSES = { 'base_prompt' : 'openstack-rb5', 'config_mode' : '(config)', } net_connect = ConnectHandler(**brocade_vdx) # Enter enable mode module.prompt_initial = net_connect.find_prompt() net_connect.enable() module.enable_prompt = net_connect.find_prompt() # Send a set of config commands module.config_mode = net_connect.config_mode() config_commands = ['logging raslog console WARNING', 'interface vlan 20', 'banner motd test_message'] net_connect.send_config_set(config_commands) # Exit config mode module.exit_config_mode = net_connect.exit_config_mode() # Verify config changes module.config_commands_output = net_connect.send_command('show vlan brief') net_connect.disconnect()
def show_ip_int_br(current_operator, current_device): command = "show ip interface brief" net_connect = ConnectHandler( device_type=device_type, ip=current_device.ip, username=current_operator.username, password=current_operator.password ) net_connect.find_prompt() output = net_connect.send_command(command) print output
def show_version(current_operator, current_device): command = "show version" net_connect = ConnectHandler( device_type=device_type, ip=current_device.ip, username=current_operator.username, password=current_operator.password ) net_connect.find_prompt() output = net_connect.send_command(command) print output
def main(): #password = getpass() password = '******' # Define libraries for devices we will connect to pynet1 = { 'device_type': 'cisco_ios', 'ip': '50.76.53.27', 'username': '******', 'password': password, 'port': 22, } pynet2 = { 'device_type': 'cisco_ios', 'ip': '50.76.53.27', 'username': '******', 'password': password, 'port': 8022, } srx = { 'device_type': 'juniper', 'ip': '50.76.53.27', 'username': '******', 'password': password, 'port': 9822, } pynet_rtr2 = ConnectHandler(**pynet2) pynet_rtr2.config_mode() outp = pynet_rtr2.find_prompt() print outp outp2 = pynet_rtr2.check_config_mode() print 'Config mode status is ' + str(outp2)
def main(): device_list = [cisco_ios, cisco_xr, arista_veos] start_time = datetime.now() print for a_device in device_list: as_number = a_device.pop('as_number') net_connect = ConnectHandler(**a_device) net_connect.enable() print "{}: {}".format(net_connect.device_type, net_connect.find_prompt()) if check_bgp(net_connect): print "BGP currently configured" remove_bgp_config(net_connect, as_number=as_number) else: print "No BGP" # Check BGP is now gone if check_bgp(net_connect): raise ValueError("BGP configuration still detected") # Construct file_name based on device_type device_type = net_connect.device_type file_name = 'bgp_' + device_type.split("_ssh")[0] + '.txt' # Configure BGP output = configure_bgp(net_connect, file_name) print output print print "Time elapsed: {}\n".format(datetime.now() - start_time)
def main(): """Exercises using Netmiko""" # 99saturday sw1_pass = getpass("Enter switch password: "******"Current Prompt: " + net_connect.find_prompt() print "\nConfiguring VLAN" print "#" * 80 output = net_connect.send_config_set(cfg_commands) print output print "#" * 80 print print "\nConfiguring VLAN from file" print "#" * 80 output = net_connect.send_config_from_file("vlan_cfg.txt") print output print "#" * 80 print
def main(): print 'Username:'******'device_type':DEV_TYPE[i], 'ip':DEVICES[i], 'username':usern, 'password':passwd } print '>>>>>>>>>>>>> DEVICE: {} <<<<<<<<<<<<<<'.format(DEVICES[i]) # Initiating netmiko connection net_connect = ConnectHandler(**net_dev) resp = net_connect.find_prompt() print resp # Entering config mode, deploying commands and exiting config mode resp = net_connect.send_config_set(CONF_COMMANDS) print resp except Exception as e: print e exit(0)
def connect_net_device(**kwarg): net_connect = ConnectHandler(**kwarg) print "Current prompt: {}".format(net_connect.find_prompt()) show_run = net_connect.send_command("sh run") print "sh run output: \n\n{}".format(show_run) filename = net_connect.base_prompt + ".txt" save_file (filename , show_run)
def setup_module(module): module.EXPECTED_RESPONSES = { 'enable_prompt' : 'xe-test-rtr#', 'base_prompt' : 'xe-test-rtr', 'interface_ip' : '172.30.0.167', 'config_mode' : '(config)', } show_ver_command = 'show version' module.basic_command = 'show ip int brief' net_connect = ConnectHandler(**cisco_xe) module.show_version = net_connect.send_command(show_ver_command) module.show_ip = net_connect.send_command(module.basic_command) net_connect.enable() module.enable_prompt = net_connect.find_prompt() module.config_mode = net_connect.config_mode() config_commands = ['logging buffered 20000', 'logging buffered 20010', 'no logging console'] net_connect.send_config_set(config_commands) module.exit_config_mode = net_connect.exit_config_mode() module.config_commands_output = net_connect.send_command('show run | inc logging buffer') net_connect.disconnect()
def setup_module(module): module.EXPECTED_RESPONSES = { 'enable_prompt' : 'n7k1#', 'base_prompt' : 'n7k1', 'interface_ip' : '10.3.3.245', 'config_mode' : '(config)' } show_ver_command = 'show version' module.basic_command = 'show ip int brief' net_connect = ConnectHandler(**cisco_nxos) module.show_version = net_connect.send_command(show_ver_command) module.show_ip = net_connect.send_command(module.basic_command) net_connect.enable() module.enable_prompt = net_connect.find_prompt() module.config_mode = net_connect.config_mode() config_commands = ['logging monitor 3', 'logging monitor 7', 'no logging console'] net_connect.send_config_set(config_commands) module.exit_config_mode = net_connect.exit_config_mode() module.config_commands_output = net_connect.send_command("show run | inc 'logging monitor'") net_connect.disconnect()
def main(): try: hostname = raw_input("Enter remote host to test: ") except NameError: hostname = input("Enter remote host to test: ") home_dir = path.expanduser("~") key_file = "{}/.ssh/cisco_rsa".format(home_dir) cisco_test = { "ip": hostname, "username": "******", "device_type": "cisco_ios", "use_keys": True, "key_file": key_file, "verbose": False, } net_connect = ConnectHandler(**cisco_test) print() print("Checking prompt: ") print(net_connect.find_prompt()) print() print("Testing show ip int brief: ") output = net_connect.send_command("show ip int brief") print(output) print()
def main(): """Exercises using Netmiko""" rtr1_pass = getpass("Enter router password: "******"Enter switch password: "******"Current Prompt: " + net_connect.find_prompt() show_arp = net_connect.send_command("show arp") print print '#' * 80 print show_arp print '#' * 80 print show_run = net_connect.send_command("show run") filename = net_connect.base_prompt + ".txt" print "Save show run output: {}\n".format(filename) save_file(filename, show_run)
def get_cdp_neighbor_details(ip, username, password, enable_secret): """ get the CDP neighbor detail from the device using SSH :param ip: IP address of the device :param username: username used for the authentication :param password: password used for the authentication :param enable_secret: enable secret :return: """ # establish a connection to the device ssh_connection = ConnectHandler( device_type='cisco_ios', ip=ip, username=username, password=password, secret=enable_secret ) # enter enable mode ssh_connection.enable() # prepend the command prompt to the result (used to identify the local device) result = ssh_connection.find_prompt() + "\n" # execute the show cdp neighbor detail command # we increase the delay_factor for this command, because it take some time if many devices are seen by CDP result += ssh_connection.send_command("show cdp neighbor detail", delay_factor=2) # close SSH connection ssh_connection.disconnect() return result
def main(): router1 = { 'device_type': 'cisco_ios', 'ip': '10.9.0.67', 'username': '******', 'password': '******' } router2 = { 'device_type': 'cisco_ios', 'ip': '10.63.176.57', 'username': '******', 'password': '******' } routers = [router1, router2] for router in routers: # Connect to device device_conn = ConnectHandler(**router) # Change config from file device_conn.send_config_from_file(config_file='config_commands.txt') # Validate changes print ">>> " + device_conn.find_prompt() + " <<<" #output = device_conn.send_command("show run | in logging", delay_factor=.5) output = device_conn.send_command_expect("show run | in logging") print output + "\n\n" # Close connection device_conn.disconnect()
def main(): inputfile, config_commands = get_cmd_line() print("Switch configuration updater. Please provide login information.\n") # Get username and password information. username = input("Username: "******"Password: "******"Enable Secret: ") print("{}{:<20}{:<40}{:<20}".format( "\n", "IP Address", "Name", "Results"), end="") for switchip in inputfile: ciscosw = { "device_type": "cisco_ios", "ip": switchip.strip(), "username": username.strip(), "password": password.strip(), "secret": enasecret.strip(), } print() print("{:<20}".format(switchip.strip()), end="", flush=True) try: # Connect to switch and enter enable mode. net_connect = ConnectHandler(**ciscosw) except Exception: print("** Failed to connect.", end="", flush=True) continue prompt = net_connect.find_prompt() # Print out the prompt/hostname of the device print("{:<40}".format(prompt), end="", flush=True) try: # Ensure we are in enable mode and can make changes. if "#" not in prompt[-1]: net_connect.enable() print("#", end="", flush=True) except Exception: print("Unable to enter enable mode.", end="", flush=True) continue else: for cmd in config_commands: # Make requested configuration changes. try: if cmd in ("w", "wr"): output = net_connect.save_config() print("w", end="", flush=True) else: output = net_connect.send_config_set(cmd) if "Invalid input" in output: # Unsupported command in this IOS version. print("Invalid input: ", cmd, end="", flush=True) print("*", end="", flush=True) except Exception: # Command failed! Stop processing further commands. print("!") break net_connect.disconnect()
def set_interface_description(current_operator, current_device, device_interface): command_set = [ "interface " + device_interface, "description Aiport Express WAN" ] net_connect = ConnectHandler( device_type=device_type, ip=current_device.ip, username=current_operator.username, password=current_operator.password ) net_connect.find_prompt() output = net_connect.send_config_set(command_set) print output
def main(): device_list = [cisco_ios, cisco_xr, arista_veos] start_time = datetime.now() print for a_device in device_list: net_connect = ConnectHandler(**a_device) print "{}: {}".format(net_connect.device_type, net_connect.find_prompt()) print "Time elapsed: {}\n".format(datetime.now() - start_time)
def main(): pynet2 = { 'device_type': 'cisco_ios', 'ip' : '50.76.53.27', 'username' : 'pyclass', 'port' : 8022, 'password' : '88newclass' } pynet_rtr2 = ConnectHandler(**pynet2) pynet_rtr2.config_mode() if pynet_rtr2.check_config_mode(): print "We're in config mode" else: print "Nope, We're NOT in config mode" print pynet_rtr2.find_prompt() pynet_rtr2.exit_config_mode() print pynet_rtr2.find_prompt()
def connect(host): ssh_connection = ConnectHandler(**host) output = ssh_connection.send_command('show arp') hostname = ssh_connection.find_prompt() time.sleep(1) print '*' * 35 + " ARP Table from device %s " % hostname + '*' * 35 print output print '*' * 105
def connect_ssh(target_ip, username, password, DEBUG, output): if DEBUG: verbose = True else: verbose = False dns_list = list() for i in target_ip: try: # Define variables for function x, z, v = 0, 1, 2 vlan_list = list() output += ("*" * 4 + " START device: %s " + "-" * 35 + ">" * 4 + "\n") % i ssh_con = ConnectHandler(device_type='hp_procurve', ip=i, username=username, password=password, verbose=verbose) # Find hostname will be used for first part of VLAN IP dns name output += "\nFind prompt: \n" output_cmd = ssh_con.find_prompt() hostname = output_cmd if DEBUG: print hostname output += output_cmd # Get VLAN information from switch output_cmd = ssh_con.send_command('show vlan custom id name ipaddr') vlan_output = output_cmd.split(' ') output += output_cmd # Clean output information vlan_output = filter(None, vlan_output) # Filter VLAN ID's for y in vlan_output[18:-1]: if y != '\n': vlan_list.append(y) if DEBUG: print vlan_list lp = len(vlan_list) / 3 while lp != 0: vlan_name = hostname[:-1] + '-' + 'VL' + vlan_list[x] + '-' + vlan_list[z] dns_list.append(vlan_name) dns_list.append(vlan_list[v]) x += 3 z += 3 v += 3 lp -= 1 if DEBUG: print dns_list ssh_con.disconnect() except: print "Problem connecting with ip: %s" % (i) continue return output, dns_list
def print_output(device, mp_queue, command="show ver"): ''' it execute a command in Netmiko and print out the result ''' pynet_rtr = ConnectHandler(**device) output = pynet_rtr.send_command(command) to_display = "\n Device: {} ========> {} \n".format(pynet_rtr.find_prompt(),\ pynet_rtr.ip) to_display += output to_display += '*'*80 +"\n"*2 mp_queue.put(to_display)
def getinfo(username, password, host, commands, mode): global seen_before all_output = [] row = [] if mode == "SSH": device = { 'device_type': 'cisco_ios', 'ip': host, 'username': username, 'password': password, 'port': 22, # optional, defaults to 22 'secret': '', # optional, defaults to '' 'verbose': False, # optional, defaults to False } elif mode == "Telnet": device = { 'device_type': 'cisco_ios_telnet', 'ip': host, 'username': username, 'password': password, 'port': 23, # optional, defaults to 22 'secret': '', # optional, defaults to '' 'verbose': False, # optional, defaults to False } print('Connecting to %s with %s' % (host, mode)) # Create instance of SSHClient object net_connect = ConnectHandler(**device) # Automatically add untrusted hosts (make sure okay for security policy in your environment) print('%s connection established to %s' % (mode, host)) # Use invoke_shell to establish an 'interactive session' a = re.search(r'(.*)[#>]', net_connect.find_prompt()) hostname = a.group(1) seen_before.append(hostname) ip_lines = net_connect.send_command('show ip int brief | ex unass') for line in ip_lines.split('\n'): a = re.search(r'([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', line) if a is not None: seen_before.append(a.group(1)) for command in commands: output = net_connect.send_command(command) all_output.append(output) if command == 'show inventory': lines = output.split('\n\n') for line in lines: new_line = line.replace('\n', '') a = re.search(r'NAME:\ ?(\S+).*PID:\ ?(\S+).*SN:\ ?(\S+)', new_line) if a is not None: pid = a.group(2) serial = a.group(3) row.append([hostname, pid, serial]) if verbose_mode: print(output) return all_output, row
def main(): ''' Logs into each router and collects the arp table ''' print '=' *80 for device in (pynet1,pynet2,pynetsrx1): ssh_conn = ConnectHandler(**device) output = ssh_conn.send_command(cmd) prompt = ssh_conn.find_prompt() print '\n %s from %s \n %s' %(cmd, prompt, output) print print '=' *80
def main(): ''' Logs into each router and executes commands cmd and ver ''' print '=' *80 ssh_conn = ConnectHandler(**device) ssh_conn.config_mode() ssh_conn.send_command(cmd) output = ssh_conn.send_command(ver) prompt = ssh_conn.find_prompt() print '\n %s from %s \n %s' %(cmd, prompt, output) print print '=' *80
def main(): my_device = { 'device_type': 'cisco_ios', 'ip': '10.40.0.1', 'username': '******', 'password': '******' } # Connect to device device_conn = ConnectHandler(**my_device) # Enter Config mode device_conn.config_mode() # Verify current mode print device_conn.check_config_mode() # Print the current prompt print device_conn.find_prompt() # Close connection device_conn.disconnect()
def main(): password = getpass() for a_dict in (pynet1, pynet2, juniper_srx): a_dict['password'] = password net_connect2 = ConnectHandler(**pynet2) net_connect2.config_mode() print "Config mode:{}".format(net_connect2.check_config_mode()) print "Current prompt: {}".format(net_connect2.find_prompt())
"""Now use send_command() and the expect_string argument to handle the additional prompting. Once again specify a target IP address of '8.8.8.8'.""" from netmiko import ConnectHandler from getpass import getpass cisco_dict = { "host": "cisco4.lasthop.io", "username": "******", "password": getpass(), "device_type": "cisco_ios" } ios_connect = ConnectHandler(**cisco_dict) output = ios_connect.find_prompt() output += ios_connect.send_command("ping", strip_command=False, expect_string="Protocol") output += ios_connect.send_command("\n", strip_command=False, expect_string="address") output += ios_connect.send_command("8.8.8.8", strip_command=False, expect_string="count") output += ios_connect.send_command("\n", strip_command=False, expect_string="Datag") output += ios_connect.send_command("\n", strip_command=False, expect_string="seconds") output += ios_connect.send_command("\n", strip_command=False,
import yaml import os from netmiko import ConnectHandler #filename = "/home/mfeindt/.netmiko.yml" filename = os.path.join(os.path.expanduser("~"), ".netmiko.yml") with open(filename, 'r') as f: inv_data = yaml.load(f) cisco_ios3 = inv_data['cisco3'] cisco_ios3["session_log"] = "my_session.txt" ios_connect = ConnectHandler(**cisco_ios3) delimit = "-------------" print(delimit) print(ios_connect.find_prompt()) print(delimit) ios_connect.disconnect()
from netmiko import ConnectHandler Tshoot_Frame_Relay_Switch = { 'device_type': 'cisco_ios', 'ip':'192.100.101.201', 'username': '******', 'password': '******', 'secret': 'cisco', 'port':2045, 'global_delay_factor':5 } with open('C:\Users\user1\Desktop\DC_Automation_Scripts\Tshoot_Pod_Config\FR-SW.txt') as f: lines = f.read().splitlines() print lines net_connect = ConnectHandler(**Tshoot_Frame_Relay_Switch) net_connect.enable() print net_connect.find_prompt() print "Sesion Estabilished with Tshoot_Frame_Relay_Switch" output = net_connect.send_config_set(config_commands=lines, max_loops=10000, delay_factor=5) print output
saveresults = "Y" saveresultslist = [] for sshdevice in verificationcsv: sshdevicename = sshdevice[:1][0] sshdeviceip = sshdevice[1:][0] sshdevicevendor = sshdevice[2:][0] sshdevicetype = sshdevice[3:][0] sshdevicetype = sshdevicevendor.lower() + "_" + sshdevicetype.lower() #Start Connection try: sshnet_connect = ConnectHandler(device_type=sshdevicetype, ip=sshdeviceip, username=sshusernameq, password=sshpasswordq, secret=sshenpasswordq) sshdevicehostname = sshnet_connect.find_prompt() sshdevicehostname = sshdevicehostname.strip('#') if '>' in sshdevicehostname: sshnet_connect.enable() sshdevicehostname = sshdevicehostname.strip('>') print 'Successfully connected to ' + sshdevicehostname for pinglist in verificationcsv: pinglistname = pinglist[:1][0] pinglistip = pinglist[1:][0] pingcheckarp = "ping " + pinglistip + " timeout 100 repeat 1" pingcheck = "ping " + pinglistip + " timeout 500 repeat 1" sshnet_connect.send_command(pingcheckarp) pingresult = sshnet_connect.send_command(pingcheck) if "!" in pingresult: print "Success = " + sshdevicename + "->" + pinglistname if "Y" in saveresults:
from netmiko import ConnectHandler device = ConnectHandler(device_type='cisco_ios', ip='192.168.101.11', username='******', password='******', secret='cisco123') SendingCommands = ["username Markus secret cisco123"] print(device.find_prompt()) device.enable() print(device.find_prompt()) device.config_mode() print(device.find_prompt()) output = device.send_config_set(SendingCommands) print(output) device.disconnect()
def ssh_conn(device): net_connect = ConnectHandler(**device) return net_connect.find_prompt()
from netmiko import ConnectHandler cisco_cloud_router = { 'device_type': 'cisco_ios', 'ip': '10.0.0.5', 'username': '******', 'password': '******' } connection = ConnectHandler(**cisco_cloud_router) print(connection) print(type(connection)) interface_output = connection.send_command('show run int g1') print(interface_output) hostname = connection.find_prompt() print(hostname[:-1]) interface_name = connection.send_command('show run int g2 | i ^interface') print(interface_name)
def backend(src, dst): src = src dst = dst arr = [] count = 0 setofnames = set() dictofnames = {} dictofobj = {} name = '' s = {src} now = src honame = set() exit = dict() entry = dict() entryrev = dict() ls = [] ls.append(now) extract = set() p = '' boo = True intojson = [] while (len(s) > 0): now = ls[0] boo = True while boo: try: #device = {"device_type": "autodetect","host":now,"username": "******","password":"******"} #guesser = SSHDetect(**device) #best_match = guesser.autodetect() #print(best_match,guesser.potential_matches) ssh = ConnectHandler(device_type="cisco_ios", host=now, username="******", password="******") boo = False except Exception as e: boo = True print(" Connection error, trying again ", e) #ret=ssh.send_command("en") boo = True while boo: try: name = ssh.find_prompt() boo = False except Exception as e: print(str(e)) print("Trying again") boo = True if not (re.match("^[A-Z]{3}-{2}[A-Z]{2}[0-9]{2}#{1}$", name)): boo = True print(" Name Received Incorrect- Trying again " + name) name = name[:-1] if name not in setofnames: setofnames.add(name) dictofnames[name] = count k = router(name) arr.append(k) dictofobj[name] = k dictofobj[name].addconnect(ssh) dictofobj[name].addsship(now) count += 1 print(name) honame.add(name) print("dict of names ") print(dictofnames) boo2 = True while boo2: boo = True while boo: try: ret = ssh.send_command("sh ip route " + dst + " | include Known via") boo = False except Exception as e: boo = True print("1 Exception Handled- Trying again") print(" return from sh ip route | inc known via ") print(ret) if not ret: print("1 Trying again") boo = True elif isinstance(ret, list): print("1 Return from sh ip route is a list, trying again") boo = True elif len(ret.split()) >= 3: boo = False else: print("1 Trying Again sh ip route") boo = True print(" Name " + name + " show ip route | i known via") print(ret) ret = ret.split() prot = ret[2][1:] print("PROT- " + prot) if prot != 'bgp' and prot != 'connected",' and prot != 'eigrp': boo2 = True print(" Protocol received isn't correct. Trying Again ") else: boo2 = False if prot == 'bgp': dst1 = dst fl = 0 print("Prot BGP") while fl != 2: boo = True while boo: try: ret = ssh.send_command("sh ip route " + dst1) boo = False except: boo = True print("2 Exception Handled- Trying again") if not ret: boo = True print("2 Trying again") elif isinstance(ret, list): print( "2 Return from sh ip route is a list, trying again" ) boo = True elif len(ret.split()) > 3: boo = False else: boo = True print("2 Trying again") print("\tBGP- sh ip route for dst " + dst1) print(ret) ret = ret.split("\n") fl = 0 for i in ret: i = i.split() print("splitting ret") print(i) if i[0] == '*': nxt = i[1] if nxt == 'directly': x = i.index('via') hop = i[x + 1] fl = 2 break elif re.match('^(?:[0-9]{1,3}\.){3}([0-9]{1,3})', nxt): dst1 = nxt if nxt[-1] == ',': dst1 = nxt[:-1] fl = 1 break print("Name " + name + " BGP: next hop " + dst1 + " exit interface " + hop) extract.add(dst1) s.add(dst1) ls.append(dst1) p = '' p = hop + ' ' + dst1 if name not in exit.keys(): exit[name] = set() exit[name].add(p) boo = True while boo: try: ret = ssh.send_command("sh ip int brief | include " + hop) boo = False except: print("3 Exception Handled- Trying again") boo = True if not ret: boo = True elif isinstance(ret, list): print( "3 Return from sh ip int brief is a list, trying again" ) boo = True elif len(ret.split()) < 6: boo = True else: boo = False ip = ret.split()[1] ctobj = dictofnames[name] arr[ctobj].addexit(p) dictofobj[name].adddictip(hop, ip) p = '' elif prot == 'connected",': boo = True while boo: try: ret = ssh.send_command("sh ip route " + dst + " | include directly") boo = False except: boo = True print("4 Exception Handled- Trying again") print(" Return from sh ip route dst i directly") print(ret) if not ret: boo = True print( "4 Return from show ip route dst is null, Trying again" ) elif isinstance(ret, list): print( "4 Return from sh ip route directly is a list, trying again" ) boo = True elif len(ret.split()) > 3: boo = False else: print("4 Trying again") boo = True print("Connected route- show ip route| i directly ") print(ret) ret = ret.split() p = '' x = ret.index('via') p = ret[x + 1] hop = ret[x + 1] p = p + ' directly' if name not in exit.keys(): exit[name] = set() exit[name].add(p) print(" Name " + name + " is connected to dst via " + p) ctobj = dictofnames[name] arr[ctobj].addexit(p) boo = True while boo: try: ret = ssh.send_command("sh ip int brief | include " + hop) boo = False except: print("5 Exception Handled- Trying again") boo = True if not ret: boo = True elif isinstance(ret, list): print( "5 Return from sh ip int brief is a list, trying again" ) boo = True elif len(ret.split()) < 6: boo = True else: boo = False ip = ret.split()[1] dictofobj[name].adddictip(hop, ip) p = '' else: boo = True while boo: try: ret = ssh.send_command("sh ip route " + dst + " | include via") boo = False except: boo = True print("6 Exception Handled- Trying again") print(" Return from sh ip route") print(ret) if not ret: boo = True print("6 Trying again") elif isinstance(ret, list): print("6 Return from sh ip route is a list, trying again") boo = True elif len(ret.split()) > 3: boo = False else: print("6 Trying again") boo = True print("output from sh ip route | inc via ") print(ret) print("Splitting") ret = ret.split('\n') for i in ret: i = i.split() print(i) t = 0 for j in i: #print(j) if re.match('^(?:[0-9]{1,3}\.){3}([0-9]{1,3})', j): print("extract- " + j[:-1]) j = j[:-1] if j not in extract: extract.add(j) s.add(j) ls.append(j) t = 1 if t == 1: num = i.index('via') p = i[num + 1] hop = i[num + 1] p = p + ' ' + j if name not in exit.keys(): exit[name] = set() exit[name].add(p) ctobj = dictofnames[name] #print("ctobj "+ctobj) arr[ctobj].addexit(p) print("hop ", hop) boo = True ret1 = "" while boo: try: ret1 = ssh.send_command( "sh ip int brief | include " + hop) boo = False except: print("6-2 Exception Handled- Trying again") boo = True print(" Return ") print(ret1) print(len(ret1.split())) #print(ret1.split()[0]) if not ret1: boo = True elif isinstance(ret1, list): print( "6-2 Return from sh ip int brief is a list, trying again" ) boo = True elif len(ret1.split()) < 5: boo = True else: boo = False ip = ret1.split()[1] dictofobj[name].adddictip(hop, ip) p = '' break extract.clear() s.remove(now) ls.remove(now) if now != src: boo = True while boo: try: ret = ssh.send_command("sh ip int brief | include " + now) boo = False except: print("7 Exception Handled- Trying again") boo = True print(" return from sh ip int brief | inc dest at dest ") print(ret) print(len(ret.split())) if not ret: print("7 null") boo = True elif isinstance(ret, list): print("7 Return from sh ip route is a list, trying again") boo = True elif len(ret.split()) < 6: boo = True print("7 Trying Again") else: boo = False print(" Name " + name + " sh ip int brief | include " + now) print(ret) ret = ret.split() if name not in entry.keys(): entry[name] = set() p = '' p = ret[0] hop = ret[0] ip = now p = p + ' ' + now entry[name].add(p) ctobj = dictofnames[name] arr[ctobj].addentry(p) dictofobj[name].adddictip(hop, ip) entryrev[now] = set() p = '' p = name + ' ' + ret[0] entryrev[now].add(p) boo = True while boo: try: #device = {"device_type": "autodetect","host":dst,"username": "******","password":"******"} #guesser = SSHDetect(**device) #best_match = guesser.autodetect() #print(best_match,guesser.potential_matches) ssh = ConnectHandler(device_type="cisco_ios", host=dst, username="******", password="******") boo = False except Exception as e: boo = True print(" Connection error, trying again ", e, dst) boo = True while boo: try: name = ssh.find_prompt() boo = False except Exception as e: print(str(e)) print("Find Prompt errTrying Again") boo = True name = name[:-1] honame.add(name) if name not in setofnames: setofnames.add(name) dictofnames[name] = count k1 = router(name) arr.append(k1) dictofobj[name] = k1 dictofobj[name].addconnect(ssh) count += 1 boo = True while boo: try: ret = ssh.send_command("sh ip int brief | include " + dst) boo = False except: print("8 Exception Handled- Trying again") boo = True print(" return from sh ip int brief | inc dest ") print(ret) if not ret: boo = True elif isinstance(ret, list): print("8 Return from sh ip int brief is a list, trying again") boo = True elif len(ret.split()) < 6: boo = True print("8 Trying Again") else: boo = False ret = ret.split() #print(ret) if name not in entry.keys(): entry[name] = set() p = '' p = ret[0] p = p + ' ' + 'directly' entry[name].add(p) hop = ret[0] ip = dst ctobj = dictofnames[name] arr[ctobj].addentry(p) dictofobj[name].adddictip(hop, ip) p = '' entryrev['directly'] = set() p = name + ' ' + ret[0] entryrev['directly'].add(p) print("Entry interfaces ") print(entry) print() print(" Exit interfaces ") print(exit) print() print(" Entry Reverse ") print(entryrev) ff = 0 for nme in setofnames: ssh = dictofobj[nme].handle #general_node_parameters boo = True while boo: ff = 0 try: ret = ssh.send_command("sh version", use_textfsm=True) print("Sh version Exec") boo = False except Exception as e: print(" 9-0 Exception raised is show version, trying again ") print(e) boo = True ff = 1 try: #device = {"device_type": "autodetect","host":dictofobj[nme].sship,"username": "******","password":"******"} #guesser = SSHDetect(**device) #best_match = guesser.autodetect() #print(best_match,guesser.potential_matches) ssh = ConnectHandler(device_type="cisco_ios", host=dictofobj[nme].sship, username="******", password="******") except Exception as ee: print("Exception raised again") print(ee) print("Return from Show version") print(ret) if ff == 1: boo = True elif not ret: print(" 9-0 Return from show version is null. Trying again ") boo = True elif not (isinstance(ret, list)): print( " 9-0 Return from show version is not proper. Trying again " ) boo = True else: boo = False verdict = {} verdict['soft_ver'] = ret[0]['soft_ver'] verdict['version'] = ret[0]['version'] verdict['uptime'] = ret[0]['uptime'] verdict['hardware'] = ret[0]['hardware'][0] verdict['reload_reason'] = ret[0]['reload_reason'] dictofobj[nme].gennodedict['version'] = verdict boo = True while boo: try: ret = ssh.send_command("sh proc cpu | ex 0.0", use_textfsm=True) boo = False except: print("9 Exception Raised , Trying again") boo = True if not (isinstance(ret, list)): boo = True print("9 return from sh proc cpu not proper, trying again") else: boo = False #parse the return from show environment and take out parameters like ct1 = 0 for line in ret: if ct1 == 0: cpu = {} cpu['cpu_5_sec'] = line['cpu_5_sec'] cpu['cpu_1_min'] = line['cpu_1_min'] cpu['cpu_5_min'] = line['cpu_5_min'] dictofobj[nme].gennodedict['CPU'] = cpu combine = {} combine['process'] = line['process'] combine['proc_5_sec'] = line['proc_5_sec'] combine['proc_1_min'] = line['proc_1_min'] combine['proc_5_min'] = line['proc_5_min'] dictofobj[nme].gennodedict[line['pid']] = combine ct1 += 1 #parsing sh ip route boo = True while boo: try: ret = ssh.send_command("sh ip route") boo = False except: print("10 Exception Raised , Trying again") boo = True print(ret) if not ret: boo = True elif isinstance(ret, list): print("10 Return from sh ip route is a list, trying again") boo = True else: boo = False ret = ret.split('\n') gen = {} ct1 = 0 print("RETURN: ", ret) for line in ret: print("LINE: ", line) line2 = line.split() print("Splitted LINE: ", line2) if not (not (line2) ) and line2[0] != 'S' and line2[0] != 'C' and line2[ 0] != 'S*' and 'via' in line2 and line2[ 0] == 'D' and line2[0] == 'B': pos = line2.index('via') if line2[pos + 2][0:2] == '00': ct1 += 1 gen[ct1] = line print(line) dictofobj[nme].gennodedict['ip_route_00'] = gen #keys for gen dict is just numbers with no significance. display only values #dictofobj[nme].gennodedict['redundant_power']= #-----------------------------------------Harshad------------------------------------------------------------------------------------------ boo = True while boo: ans = ans1 = 0 try: ans = ssh.send_command("show ip protocols | include bgp") ans1 = ssh.send_command("show ip protocols | include eigrp") boo = False except: print("9-2 Exception raised in sh ip protocols, trying again ") boo = True print(" sh ip protocols | i bgp ") print(ans) print(" sh ip protocols | i eigrp") print(ans1) if not (isinstance(ans, str)) or not (isinstance(ans1, str)): boo = True print( "9-2 Return from sh ip protocols not proper. Trying again") elif not ans and not ans1: print("9-2 Return null from both protocols, trying again ") boo = True elif (not (ans1) and len(ans.split()) < 5) or (not (ans) and len(ans1.split()) < 5): print( " 9-2-1 Return from sh ip protocols not proper. Trying again" ) boo = True elif not (not (ans)) and len(ans.split()) < 5 and not ( not (ans1)) and len(ans1.split()) < 5: print( " 9-2-3 Return from sh ip protocols not proper. Trying again" ) boo = True else: boo = False bgp = ans.split("\n") eigrp = ans1.split("\n") bgp_sub = '"bgp' eigrp_sub = '"eigrp' flag1 = 0 flag2 = 0 for text in bgp: if bgp_sub in text: flag1 = 1 break for text in eigrp: if eigrp_sub in text: flag2 = 1 break if flag2 == 1: print("eigrp there") #call bgp func boo = True while boo: try: ans = ssh.send_command("show ip eigrp neighbors") boo = False except: print( "9-3 Exception handled. Error in sh ip eigrp neigh. Trying again " ) boo = True print(" Return from sh ip eigrp neighbors ") print(ans) if not ans: print('Null returned from show ip eigrp neighbors') boo = True elif not (isinstance(ans, str)): print( 'not a string, returned from show ip eigrp neighbors') boo = True elif len(ans.split()) < 3: print('size less, returned from show ip eigrp neighbors') boo = True else: boo = False boo = True while boo: try: template = open( 'cisco_ios_show_ip_eigrp_neighbors.template') res_template = textfsm.TextFSM(template) ans_final = res_template.ParseText(ans) boo = False except Exception as e: print(e) print("9-4 Exception in Textfsm, Trying again") boo = True print(ans_final) hello = {} j = 0 dictofobj[nme].gennodedict['eigrp_neigh'] = dict() for i in range(0, len(ans_final)): hello = {} hello['neighbor'] = ans_final[i][1] hello['uptime'] = ans_final[i][4] hello['srtt'] = ans_final[i][5] hello['rto'] = ans_final[i][6] #dictofobj[nme].gennodedict['eigrp_neigh'] dictofobj[nme].gennodedict['eigrp_neigh'][ans_final[i] [1]] = dict() dictofobj[nme].gennodedict['eigrp_neigh'][ans_final[i] [1]] = hello if flag1 == 1: print("bgp there") ans = ssh.send_command("show ip bgp summary") print(ans) template = open('cisco_ios_show_ip_bgp_summary.template') res_template = textfsm.TextFSM(template) ans_final = res_template.ParseText(ans) print(ans_final) hello = {} j = 0 dictofobj[nme].gennodedict['bgp_neigh'] = dict() for i in range(0, len(ans_final)): hello = {} hello['neighbor'] = ans_final[i][2] hello['AS'] = ans_final[i][3] hello['up/down'] = ans_final[i][5] dictofobj[nme].gennodedict['bgp_neigh'][ans_final[i] [2]] = dict() dictofobj[nme].gennodedict['bgp_neigh'][ans_final[i] [2]] = hello #---------------------------------------------------------------------------------------------------------------------------------------------- #------------------------------------------Neeraj----------------------------------------------------------------------------------------------- boo = True while boo: try: ret = ssh.send_command("show proc mem | include Total") boo = False except: print( " 9-4 Exception handled in sh proc mem | inc Pool Total. Trying Again" ) boo = True print("Return from show proc mem ") print(ret) if not ret: print(" 9-4 Returned value is null. Trying again ") boo = True elif not (isinstance(ret, str)): boo = True print("9-4 Returned value is not string, trying again ") elif ret.split()[0] != 'Processor': print( "9-4 Returned value on show proc mem is not proper, trying again" ) elif len(ret.split()) < 6: print( "9-4 Returned value on show proc mem is not proper, trying again" ) boo = True else: boo = False def mb(str): return round(int(str) / 1024 / 1024, 2) #return 1 def percent(a, b): return round((int(a) / int(b)) * 100, 2) memory = dict() ret = ret.split('\n') count = 0 for line in ret: count = count + 1 if (count > 2): break temp_vals = line.split(' ') vals = [] for string in temp_vals: if len(string.strip()) > 0: vals.append(string) print(vals) memory.update({ vals[0]: { 'total': mb(vals[3]), 'used': mb(vals[5]), 'free': mb(vals[7]), 'percent': percent(vals[5], vals[3]) } }) dictofobj[nme].gennodedict['Process_Memory'] = dict() dictofobj[nme].gennodedict['Process_Memory'] = memory print(memory) boo = True while boo: try: time1 = ssh.send_command("show clock") boo = False except: print("9-4 Exception handled. sh clock. Trying again") boo = True print("Time ") print(time1) if not time1: boo = True print("9-4 Return from show clock not proper. Trying again ") elif not (isinstance(time1, str)): print( "9-4 Return from show clock in not string. Trying again") boo = True elif len(time1.split()) < 5: print("Time received not proper. Trying again ") boo = True else: boo = False time1 = time1.split(" ")[3:5] time1 = time1[0] + " " + time1[1] print(time1) #ret = src.send_command("show log | i down|Down|up|Up|err|fail|Fail|drop|crash|MALLOCFAIL|duplex",time[0]+" "+str((int(time[1])-1))) boo = True while boo: try: ret = ssh.send_command( "show log | i down|Down|err|fail|Fail|drop|crash|MALLOCFAIL|" ) boo = False except: print("9-5 exception handled in show log. Trying again ") boo = True if not (isinstance(ret, str)): print("9-5 Return from show log in not string. Trying again") boo = True else: boo = False array = ret.split('\n') count = 0 syslog = dict() for line in array: if line.find('%') != -1 and (line.find("NBRCHANGE") != -1 or line.find("ADJCHANGE") != -1 or line.find("UPDOWN") != -1 or line.find("duplex") != -1): syslog.update({count: line}) count += 1 dictofobj[nme].gennodedict['log'] = syslog #--------------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------ARAVIND----------------------------------------------------------------------------- map_return = {} print("For Spanning Tree KPI") print(dictofobj[nme].gennodedict['version']['hardware']) output_span = '' if dictofobj[nme].gennodedict['version']['hardware'] == '3725': boo = True while boo: try: output_span = ssh.send_command("sh spanning-tree active") boo = False except: boo = True print("9-6 Exception Raised in show spanning tree") if not output_span: print( " 9-6 Return from show spanning tree is null. Trying again " ) boo = True elif not (isinstance(output_span, str)): boo = True print( " 9-6 Return from show spanning tree is not a string. Trying again " ) elif len(output_span.split()) < 4: boo = True print( " 9-6 Return from show spanning tree is not proper. Trying again" ) else: boo = False l = output_span.split('\n') print("Spanning LIST") print(l) flag = 0 p = 12 m1 = {} for k in range(len(l) - 6): if (k == p): print(" k ") print(k) print(" p ") print(p) print(l[k]) print(l[k + 6]) m1[l[k]] = l[k + 6] p += 9 p = 12 time.sleep(20) for lo in range(0, 2): for k in range(len(l) - 6): if (k == p): print(" k ") print(k) print(" p ") print(p) # print(l[k]) # print(l[k+6]) if (m1[l[k]] != l[k + 6]): map_return[l[k]] = l[k + 6] print(l[k] + "\n" + l[k + 6]) flag = 1 else: print("No change Observed") flag = 0 p += 9 p = 12 time.sleep(20) if (flag == 0): print("No Changes in the Past 1 minute") flag = 0 print('\n\n\n') dictofobj[nme].gennodedict['spanning_tree'] = map_return print("For show interface counters") print(dictofobj[nme].gennodedict['version']['hardware']) Int_count = {} if dictofobj[nme].gennodedict['version']['hardware'] == '3725': boo = True while boo: try: command = ssh.send_command("sh int counters error | ex 0") boo = False except: boo = True print( "9-7 Exception handled - sh int counters error, Trying again" ) print("Return from show int counters error") print(command) if not command: print( "9-7 Return from sh int counters errors is null, trying again" ) boo = True elif not (isinstance(command, str)): print( "9-7 Return from sh int counters errors is not a string, trying again" ) boo = True elif len(command.split()) < 5: boo = True print( "9-7 Return from sh int counters errors is not proper, trying again" ) else: boo = False output = command.split('\n') output.pop(0) if (output[1]): for i in output: print(i + '\n') Int_count = {i: 5 for i in output} else: print("Sorry Empty") dictofobj[nme].gennodedict['interface_counters_errors'] = Int_count #--------------------------------------------------------------------------------------------------------------------------------------------------- #interface_level_details for interf in dictofobj[nme].dictint.keys(): print(interf + " in loop ") boo = True while boo: try: ret = ssh.send_command("sh interfaces " + interf, use_textfsm=True) boo = False except: print("11 Exception Raised , Trying again") boo = True continue print(ret) if not ret: boo = True elif isinstance(ret, str): print("11 output not in proper form, trying again ") boo = True else: boo = False #Parse the sh interface output and get the crc and other things out print(ret) line = {} for line in ret: x = line.keys() if 'crc' in x: dictofobj[nme].dictint[interf]['crc'] = line['crc'] if 'duplex' in x: dictofobj[nme].dictint[interf]['duplex'] = line['duplex'] if 'reliability' in x: dictofobj[nme].dictint[interf]['reliability'] = line[ 'reliability'] if 'txload' in x: dictofobj[nme].dictint[interf]['txload'] = line['txload'] if 'rxload' in x: dictofobj[nme].dictint[interf]['rxload'] = line['rxload'] if 'speed' in x: dictofobj[nme].dictint[interf]['speed'] = line['speed'] if 'collisions' in x: dictofobj[nme].dictint[interf]['collisions'] = line[ 'collisions'] if 'late_collision' in x: dictofobj[nme].dictint[interf]['late_collision'] = line[ 'late_collision'] if 'overrun' in x: dictofobj[nme].dictint[interf]['overrun'] = line['overrun'] if 'interf_reset' in x: dictofobj[nme].dictint[interf]['interf_reset'] = line[ 'interf_reset'] if 'input_errors' in x: dictofobj[nme].dictint[interf]['input_errors'] = line[ 'input_errors'] if 'output_errors' in x: dictofobj[nme].dictint[interf]['output_errors'] = line[ 'output_errors'] if 'frame' in x: dictofobj[nme].dictint[interf]['frame'] = line['frame'] if 'ignored' in x: dictofobj[nme].dictint[interf]['ignored'] = line['ignored'] if 'bandwidth' in x: dictofobj[nme].dictint[interf]['bandwidth'] = line[ 'bandwidth'] if 'ignored' in x: dictofobj[nme].dictint[interf]['output_drops'] = line[ 'output_drops'] """boo=True while boo: try: ret=ssh.send_command("sh controllers "+interf) boo=False except: print("Exception Raised 2, Trying again") boo=True if not ret: boo=True elif len(boo.split('\n'))<4: boo=True else: boo=False #parse the ret and get the required parameters dictofobj[nme].dictint[interf]['']= dictofobj[nme].dictint[interf]['']= dictofobj[nme].dictint[interf]['']= dictofobj[nme].dictint[interf]['']= dictofobj[nme].dictint[interf]['']=""" forjson = {} forjson['Name'] = dict() #forjson['Name'][0]=dict() forjson['Name']['0'] = nme forjson['Interface Dictionary'] = dict() forjson['Interface Dictionary'] = dictofobj[nme].dictint forjson['General Node'] = dict() forjson['General Node'] = dictofobj[nme].gennodedict intojson.append(forjson) ssh.disconnect() #for r in arr: # r.objprint() print("FINAL OUTPUT ") print(exit) print(entryrev) print(intojson) return exit, entryrev, intojson
} ios_l2_accesslayer3 = { 'device_type': 'cisco_ios', 'ip': '192.168.122.38', 'username':'******', 'password': '******', 'secret': 'cisco' } try: ssh_connection = ConnectHandler(**ios_l2_accesslayer1) print("connection success\n") ssh_connection.enable() print(ssh_connection.find_prompt()) ports = ["gig 0/0", "gig 0/1 ", "gig1/0"] for com in ports: commands = ['int' + " " + str(com), 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'exit'] out = ssh_connection.send_config_set(commands) time.sleep(5) ssh_connection.save_config(self, 'write mem', False, '') print(out) except: print("login failure\n") sys.exit()
#!/usr/bin/env python #Netmiko task6 a thru g from netmiko import ConnectHandler from datetime import datetime from getpass import getpass password = getpass() cisco_ios = { 'host': 'cisco4.lasthop.io', 'username': '******', 'password': password, 'secret': password, 'device_type': 'cisco_ios', 'session_log': 'my_output.txt', } net_connect = ConnectHandler(**cisco_ios) #print prompt print(net_connect.find_prompt()) #config mode net_connect.config_mode() print(net_connect.find_prompt()) #exit config mode net_connect.exit_config_mode() print(net_connect.find_prompt()) net_connect.disconnect()
'device_type': 'cisco_ios', 'ip': ip, 'username': '******', 'password': '******', 'port': 22, 'secret': 'cisco', 'verbose': True } print('Connecting to ' + ip) connection = ConnectHandler(**cisco_device) print('Entering enable mode . . .') connection.enable() output = connection.send_command('show run') #print(output) prompt = connection.find_prompt() hostname = prompt[:-1] now = datetime.datetime.now() today = str(now.year) + '-' + str(now.month) + '-' + str(now.day) file = today + '-' + hostname + '.txt' with open(file + '.txt', 'w') as backup: backup.write(output) print('Backup of ' + hostname + ' completed successfully') print('#' * 30) connection.disconnect()
} juniper_srx = { 'device_type': 'juniper', 'ip': '184.105.247.76', 'username': '******', 'password': password, 'secret': '', } pynet_rtr1 = ConnectHandler(**pynet1) #pynet_rtr2 = ConnectHandler(**pynet2) #srx = ConnectHandler(**juniper_srx) # Print router prompt print pynet_rtr1.find_prompt() # Enter configuration mode pynet_rtr1.config_mode() # Check that router is in configuration mode print pynet_rtr1.check_config_mode() print pynet_rtr1.find_prompt() # Exit out of config mode and confirm pynet_rtr1.exit_config_mode() print pynet_rtr1.find_prompt() # Send various commands to routers. Disable paging enabled by default outp = pynet_rtr1.send_command('show ip int brief') print outp
# Array of routers as defined above devices = [SR1, SR2, AGG1, AGG2, PAG1, PAG2, CSG1, CSG2, XTC, VRR] for device in devices: try: labRouter = { 'device_type': 'cisco_ios', 'ip': device.inputIpAddr, 'username': device.inputUser, 'password': device.inputPassword, 'port': PORT_NUMBER, # 'secret': '%', # Add secret password, if any 'verbose': True } connectRouter = ConnectHandler(**labRouter) prompt = connectRouter.find_prompt() print(prompt) print('Entering Privileged EXEC mode...') connectRouter.enable() fileName = device.inputHostName + '.cfg' # or '.txt', depending on file format commmandList = connectRouter.send_config_from_file(fileName) print(commmandList) connectRouter.disconnect() print('Connection to ' + device.inputHostName + ' is now closed.')
from netmiko import ConnectHandler R1 = { 'device_type':'cisco_ios', 'ip':'10.1.1.1', 'username':'******', 'password':'******', } net_connect = ConnectHandler(**R1) net_connect.find_prompt() output = net_connect.send_command("show run | inc loggin") output = net_connect.send_config_set(config_commands)
import netmiko from netmiko import ConnectHandler cisco_111= { 'device_type': 'cisco_nxos', 'ip': '172.16.1.90', 'username':'******', 'password':'******', } net_connect = ConnectHandler(**cisco_111) output = net_connect.find_prompt() output2 = net_connect.send_command("sh ip int brief") output3 = net_connect.send_command("show run | inc logging") print(output) print(output2) print(output3) ## configuration changes config_commands = ['logging buffered 19999'] # create a list of configuration commands output4 = net_connect.send_config_set(config_commands) print(output4) output5 = net_connect.send_command("show run | inc logging") # verify the changes print(output5)
class JuniperSwitchTool(): def __init__(self, **kwargs): try: self.net_connect = ConnectHandler(**kwargs) self.ip = kwargs['ip'] except: raise NetMikoTimeoutException ############################################## # search ARP table for mac based in IP address # return mac or None if not found ############################################## def mac_from_ip(self, ip_addr): cli_output = self.net_connect.send_command( "sh arp no-resolve | match " + ip_addr) #pdb.set_trace() if (cli_output == ''): print("IP Address not found " + ip_addr) return None else: # interate through output # find the line that matches IP for x in cli_output.splitlines(): entry = (x.split()) # check for 'incomplete' mac address if len(entry) > 2: # check if IP matches, if true return MAC # #pdb.set_trace() if entry[1] == ip_addr: return entry[0] ############################################# # Search mac-address table for mac address # return port or None if not found ############################################# def port_from_mac(self, mac_addr): cli_output = self.net_connect.send_command( "show ethernet-switching table | match " + mac_addr) ##pdb.set_trace() if (cli_output == ""): print("MAC Address not found") return None else: for x in cli_output.splitlines(): entry = x.split() print(entry) if len(entry) > 1 and entry[1] == mac_addr: return entry[4] return None ############################################# # gather information about a channel-group # return list of ports in group ############################################# def ports_from_aggregate(self, channel_group_num): port_list = [] command = 'sh lldp neighbors | match {}'.format(channel_group_num) output = self.net_connect.send_command(command) #pdb.set_trace() for line in output.strip('0 \n').splitlines(): if line.split()[0] is not None: port_list.append(line.split()[0]) return port_list ####################################################### # find CDP neighbor on port # function returns tuple with name,ip of CDP neighbor ####################################################### def find_neighbor(self, port): command = 'sh lldp neighbors interface {} | match "Address |System name"'.format( port) output = self.net_connect.send_command(command) if output == "": return None else: name = '' ip_addr = '' for line in output.splitlines(): #pdb.set_trace() m1 = re.search('\s+Address\s+: (.*)', line) m2 = re.search('^System name\s+: (.*)', line) if m1 is not None: ip_addr = m1.group(1) if m2 is not None: name = m2.group(1) return ((name, ip_addr)) #################################### # run functions to find port for IP # returns port or None if not found # IP must be in switch ARP table #################################### def port_from_ip(self, ip_addr): mac_addr = self.mac_from_ip(ip_addr) #pdb.set_trace() if mac_addr is None: return None port = self.port_from_mac(mac_addr) return (port) def get_switch_name(self): #command = 'show run | inc hostname' #output = self.net_connect.send_command(command) ##pdb.set_trace() output = self.net_connect.find_prompt() #pdb.set_trace() # prompt is 'username@devicename>', strip carot, split and return device name return output.strip('>').split('@')[1]
class ASADevice(BaseDevice): """Cisco ASA Device Implementation.""" vendor = "cisco" def __init__(self, host, username, password, secret="", port=22, **kwargs): super().__init__(host, username, password, device_type="cisco_asa_ssh") self.native = None self.secret = secret self.port = int(port) self.global_delay_factor = kwargs.get("global_delay_factor", 1) self.delay_factor = kwargs.get("delay_factor", 1) self._connected = False self.open() def _enable(self): warnings.warn("_enable() is deprecated; use enable().", DeprecationWarning) self.enable() def _enter_config(self): self.enable() self.native.config_mode() def _file_copy_instance(self, src, dest=None, file_system="flash:"): if dest is None: dest = os.path.basename(src) fc = FileTransfer(self.native, src, dest, file_system=file_system) return fc def _get_file_system(self): """Determines the default file system or directory for device. Returns: str: The name of the default file system or directory for the device. Raises: FileSystemNotFound: When the module is unable to determine the default file system. """ raw_data = self.show("dir") try: file_system = re.match(r"\s*.*?(\S+:)", raw_data).group(1) except AttributeError: # TODO: Get proper hostname raise FileSystemNotFoundError(hostname=self.host, command="dir") return file_system def _image_booted(self, image_name, **vendor_specifics): version_data = self.show("show version") if re.search(image_name, version_data): return True return False def _interfaces_detailed_list(self): ip_int = self.show("show interface") ip_int_data = get_structured_data("cisco_asa_show_interface.template", ip_int) return ip_int_data def _raw_version_data(self): show_version_out = self.show("show version") try: version_data = get_structured_data("cisco_asa_show_version.template", show_version_out)[0] return version_data except IndexError: return {} def _send_command(self, command, expect=False, expect_string=""): if expect: if expect_string: response = self.native.send_command_expect(command, expect_string=expect_string) else: response = self.native.send_command_expect(command) else: response = self.native.send_command_timing(command) if "% " in response or "Error:" in response: raise CommandError(command, response) return response def _show_vlan(self): show_vlan_out = self.show("show vlan") show_vlan_data = get_structured_data("cisco_ios_show_vlan.template", show_vlan_out) return show_vlan_data def _uptime_components(self, uptime_full_string): match_days = re.search(r"(\d+) days?", uptime_full_string) match_hours = re.search(r"(\d+) hours?", uptime_full_string) match_minutes = re.search(r"(\d+) minutes?", uptime_full_string) days = int(match_days.group(1)) if match_days else 0 hours = int(match_hours.group(1)) if match_hours else 0 minutes = int(match_minutes.group(1)) if match_minutes else 0 return days, hours, minutes def _uptime_to_seconds(self, uptime_full_string): days, hours, minutes = self._uptime_components(uptime_full_string) seconds = days * 24 * 60 * 60 seconds += hours * 60 * 60 seconds += minutes * 60 return seconds def _uptime_to_string(self, uptime_full_string): days, hours, minutes = self._uptime_components(uptime_full_string) return "%02d:%02d:%02d:00" % (days, hours, minutes) def _wait_for_device_reboot(self, timeout=3600): start = time.time() while time.time() - start < timeout: try: self.open() return except: # noqa E722 pass # TODO: Get proper hostname parameter raise RebootTimeoutError(hostname=self.host, wait_time=timeout) def backup_running_config(self, filename): with open(filename, "w") as f: f.write(self.running_config) @property def boot_options(self): show_boot_out = self.show("show boot | i BOOT variable") # Improve regex to get only the first boot $var in the sequence! boot_path_regex = r"Current BOOT variable = (\S+):\/(\S+)" match = re.search(boot_path_regex, show_boot_out) if match: boot_image = match.group(2) else: boot_image = None return dict(sys=boot_image) def checkpoint(self, checkpoint_file): self.save(filename=checkpoint_file) def close(self): if self._connected: self.native.disconnect() self._connected = False def config(self, command): self._enter_config() self._send_command(command) self.native.exit_config_mode() def config_list(self, commands): self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode() def enable(self): """Ensure device is in enable mode. Returns: None: Device prompt is set to enable mode. """ # Netmiko reports enable and config mode as being enabled if not self.native.check_enable_mode(): self.native.enable() # Ensure device is not in config mode if self.native.check_config_mode(): self.native.exit_config_mode() @property def facts(self): """Implement this once facts' re-factor is done. """ return {} def file_copy(self, src, dest=None, file_system=None): self.enable() if file_system is None: file_system = self._get_file_system() if not self.file_copy_remote_exists(src, dest, file_system): fc = self._file_copy_instance(src, dest, file_system=file_system) # if not self.fc.verify_space_available(): # raise FileTransferError('Not enough space available.') try: fc.enable_scp() fc.establish_scp_conn() fc.transfer_file() except: # noqa E722 raise FileTransferError finally: fc.close_scp_chan() if not self.file_copy_remote_exists(src, dest, file_system): raise FileTransferError( message="Attempted file copy, but could not validate file existed after transfer" ) # TODO: Make this an internal method since exposing file_copy should be sufficient def file_copy_remote_exists(self, src, dest=None, file_system=None): self.enable() if file_system is None: file_system = self._get_file_system() fc = self._file_copy_instance(src, dest, file_system=file_system) if fc.check_file_exists() and fc.compare_md5(): return True return False def install_os(self, image_name, **vendor_specifics): timeout = vendor_specifics.get("timeout", 3600) if not self._image_booted(image_name): self.set_boot_options(image_name, **vendor_specifics) self.reboot(confirm=True) self._wait_for_device_reboot(timeout=timeout) if not self._image_booted(image_name): raise OSInstallError(hostname=self.facts.get("hostname"), desired_boot=image_name) return True return False def open(self): if self._connected: try: self.native.find_prompt() except: # noqa E722 self._connected = False if not self._connected: self.native = ConnectHandler( device_type="cisco_asa", ip=self.host, username=self.username, password=self.password, port=self.port, global_delay_factor=self.global_delay_factor, secret=self.secret, verbose=False, ) self._connected = True def reboot(self, timer=0, confirm=False): if confirm: def handler(signum, frame): raise RebootSignal("Interrupting after reload") signal.signal(signal.SIGALRM, handler) signal.alarm(10) try: if timer > 0: first_response = self.show("reload in %d" % timer) else: first_response = self.show("reload") if "System configuration" in first_response: self.native.send_command_timing("no") self.native.send_command_timing("\n") except RebootSignal: signal.alarm(0) signal.alarm(0) else: print("Need to confirm reboot with confirm=True") def rollback(self, rollback_to): raise NotImplementedError @property def running_config(self): return self.show("show running-config", expect=True) def save(self, filename="startup-config"): command = "copy running-config %s" % filename # Changed to send_command_timing to not require a direct prompt return. self.native.send_command_timing(command) # If the user has enabled 'file prompt quiet' which dose not require any confirmation or feedback. # This will send return without requiring an OK. # Send a return to pass the [OK]? message - Increase delay_factor for looking for response. self.native.send_command_timing("\n", delay_factor=2) # Confirm that we have a valid prompt again before returning. self.native.find_prompt() return True def set_boot_options(self, image_name, **vendor_specifics): current_boot = self.show("show running-config | inc ^boot system ") file_system = vendor_specifics.get("file_system") if file_system is None: file_system = self._get_file_system() file_system_files = self.show("dir {0}".format(file_system)) if re.search(image_name, file_system_files) is None: raise NTCFileNotFoundError( # TODO: Update to use hostname hostname=self.host, file=image_name, dir=file_system, ) current_images = current_boot.splitlines() commands_to_exec = ["no {0}".format(image) for image in current_images] commands_to_exec.append("boot system {0}/{1}".format(file_system, image_name)) self.config_list(commands_to_exec) self.save() if self.boot_options["sys"] != image_name: raise CommandError( command="boot system {0}/{1}".format(file_system, image_name), message="Setting boot command did not yield expected results", ) def show(self, command, expect=False, expect_string=""): self.enable() return self._send_command(command, expect=expect, expect_string=expect_string) def show_list(self, commands): self.enable() responses = [] entered_commands = [] for command in commands: entered_commands.append(command) try: responses.append(self._send_command(command)) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) return responses @property def startup_config(self): return self.show("show startup-config")
#Loading python modules from netmiko import ConnectHandler from getpass import getpass #Variables cisco_devices = ["Example1", "Example2"] #Replace with your cisco device hostnames ssh_username = input("Username:"******"cisco_ios", session_log="Cisco_SSH_Log.txt", ) print(cisco_netconnect.find_prompt()) #Prints show version to txt file with open(device_host + "_show_version.txt", "a") as f: print(cisco_netconnect.send_command("show version"), file=f) #Disconnects session cisco_netconnect.disconnect()
'password': '******', } pynetrtr2 = { 'device_type': 'cisco_ios', 'ip': '50.76.53.27', 'username': '******', 'password': '******', 'port': 8022 } srx = { 'device_type': 'juniper', 'ip': '50.76.53.27', 'username': '******', 'password': '******', 'port': 9822 } rtr2 = ConnectHandler(**pynetrtr2) # passes in dictionary key values into the function output = rtr2.find_prompt() print output output = rtr2.config_mode() print output output = rtr2.check_config_mode() print output
This script will check if the Router interface the user enters is enabled and if not it will enable it. """ cisco_device = { 'device_type': 'cisco_ios', #device type from https://github.com/ktbyers/netmiko/blob/master/netmiko/ssh_dispatcher.py 'ip': '10.1.1.20', 'username': '******', 'password': '******', 'port': 22, #optional, default 22 'secret': 'cisco', #optional, default '' 'verbose': True #optional, default False } net_connect = ConnectHandler(**cisco_device) prompter = net_connect.find_prompt() if '>' in prompter: net_connect.enable() interface = input('Enter the enterface you want to enable:') #check the interface status output = net_connect.send_command('sh ip interface ' + interface) #if an invalid interface has been entered if 'Invalid input detected' in output: print('You entered and invalid interface') else: first_line = output.splitlines()[ 0] #1st line of the sh ip interface command output print(first_line)
class SSH_Connection(): def __init__(self, user, psw, Host): self.user = user self.psw = psw self.Host = Host def try_login(self): print("Connecting to device' " + self.Host) ip_address_of_device = self.Host ios_device = { "device_type": "cisco_ios", "ip": ip_address_of_device, "username": self.user, "password": self.psw } try: self.net_connect = ConnectHandler(**ios_device) self.Telnet_privi = True print("connection is okay") except NoValidConnectionsError: self.Telnet_privi = False print( "Unable to connect to port 22 either no route to the host or not configured" ) except NetMikoTimeoutException: self.Telnet_privi = False print("Time out") except (AuthenticationException): self.Telnet_privi = False print("Authentication failure: ") except (NetMikoAuthenticationException): self.Telnet_privi = False print("Timeout to device: ") except (EOFError): self.Telnet_privi = False print("End of file while attempting device ") except (SSHException): self.Telnet_privi = False print("End Issue. Are you sure SSH is enabled?") except Exception as unknown_error: self.Telnet_privi = False print(" Some other error: %d" % (unknown_error)) except (socket.error, socket.gaierror): self.Telnet_privi = False print("socket error") except (EOFError or SSHException): self.Telnet_privi = False print("lets see") return self.Telnet_privi def connect(self): self.try_login() if self.Telnet_privi: # will get the prompt so can extract either hostname or to know the privilage level self.prompt = self.net_connect.find_prompt() return self.prompt else: return self.Telnet_privi def Savingn_config(self, device_ipaddr): self.net_connect.send_command("terminal length 0") config_data = self.net_connect.send_command("show run") ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") config_filename = (f"SSH:{st}:config-" + device_ipaddr ) # Important - create unique configuration print("---- Writing Configuration: ", config_filename) with open(config_filename, "w") as config_out: config_out.write(config_data)
def UpdateVLANs(device, vlanfolder, vlanlist): devicehost = device.get('Hostname').encode('utf-8') deviceip = device.get('IP').encode('utf-8') devicevendor = device.get('Vendor').encode('utf-8') devicetype = device.get('Type').encode('utf-8') devicetype = devicevendor.lower() + "_" + devicetype.lower() # Build Assignment List assignmentfile = vlanfolder + '\\' + devicehost + '-vlans.txt' assignmentlist = [] assignmentfileo = open(assignmentfile, 'r') assignmentdata = assignmentfileo.readlines() assignmentfileo.close() for line in assignmentdata: linedata = line.lstrip() linedata = linedata.strip('\n') linedata = linedata.split(',') assignmentlist.append(linedata) # Build Configuration Set cmdlist = [] for line in assignmentlist: try: lineport = line[0] except: lineport = '' try: linevlan = line[1] for vlanl in vlanlist: try: vlanname = str(vlanl.get('VLAN Name').encode('utf-8')) except: vlanname = str(vlanl.get('VLAN Name')) if linevlan == vlanname: linevlan = str(vlanl.get('VLAN #')) except: linevlan = '' try: linetemplate = line[2] except: linetemplate = '' try: linedesc = line[3] except: linedesc = '' if not lineport == '': intcmd = 'interface ' + lineport cmdlist.append(intcmd) if not linevlan == '': intvlan = 'switchport access vlan ' + linevlan cmdlist.append(intvlan) if not linetemplate == '': inttemplate = 'source template ' + linetemplate cmdlist.append(inttemplate) if not linedesc == '': intdesc = 'description ' + linedesc cmdlist.append(intdesc) cmdlist.append('do wr mem') # Start Connection try: sshnet_connect = ConnectHandler(device_type=devicetype, ip=deviceip, username=sshusername, password=sshpassword, secret=enablesecret) devicehostname = sshnet_connect.find_prompt() devicehostname = devicehostname.strip('#') if '>' in devicehostname: sshnet_connect.enable() devicehostname = devicehostname.strip('>') devicehostname = sshnet_connect.find_prompt() devicehostname = devicehostname.strip('#') FullOutput = sshnet_connect.send_config_set(cmdlist) OutputLogp = vlanfolder + '\\' + devicehost + '_log.txt' if os.path.exists(OutputLogp): OutputLog = open(OutputLogp, 'a+') else: OutputLog = open(OutputLogp, 'w+') OutputLog.write( '#################################################################\n' ) OutputLog.write('Start of Configuration\n') OutputLog.write('Current Start Time: ' + str(datetime.now()) + '\n') OutputLog.write(FullOutput) OutputLog.write('\n') OutputLog.close() sshnet_connect.disconnect() except Exception as e: print 'Error with sending commands to ' + deviceip + '. Error is ' + str( e) try: sshnet_connect.disconnect() except: '''Nothing''' except KeyboardInterrupt: print 'CTRL-C pressed, exiting update of switches' try: sshnet_connect.disconnect() except: '''Nothing'''
template_render = template_read.render(lan_network=lan_net, wildcard=wild_card) Final_config_str = config_str + template_render config_push_file = config_file_dir + sol_id + '_' + branch_name + '.txt' with open(config_push_file, 'w') as conf_wr: conf_wr.write(Final_config_str) try: net_connect = ConnectHandler(**jump_server) net_connect.write_channel('ssh shebin@' + row['Lan_IP']) write_channel_op_1 = net_connect._read_channel_timing( delay_factor=1, max_loops=150) write_channel_op_2 = net_connect._read_channel_timing( delay_factor=1, max_loops=150) prompt_find_1 = net_connect.find_prompt() if ('No route to host') in write_channel_op_2: Non_Connecting_Hosts(hname=branch_name, hid=sol_id) table.add_row(sol_id, branch_name, config_unsuccess) elif ('Connection timed out') in write_channel_op_2: Non_Connecting_Hosts(hname=branch_name, hid=sol_id) table.add_row(sol_id, branch_name, config_unsuccess) elif (("Are") or ('ARE')) in prompt_find_1: net_connect.write_channel('yes') prompt_find_2 = net_connect.find_prompt() if (('Password:'******'Password:'******'PASSWORD:')) in prompt_find_2:
GDAFW, GKEFW, GKGFW, GIEFW, GIHFW, GDLFW, GJBFW, GJCFW, GDBFW, GDMFW, GIIFW, GJAFW, GJEFW, GJFFW, GDNFW, GDOFW, GDPFW, GJHFW, GDFFW, GKFFW, GIFFW, ): net_connect = ConnectHandler(**REMOTE_SITES) net_connect.enable() #get user past the enable output = net_connect.send_config_from_file('NTP_Settings') write = net_connect.send_command("write mem") print(net_connect.find_prompt()) #Prints out all the routers listed above print(output) #prints the output from the command vlan100 print(write) net_connect.disconnect()
def CFG(ip, username='******', password='******', vrf_no=2, no=0, glob_del=0): device = { 'device_type': 'cisco_nxos', 'ip': ip, 'username': username, 'password': password } if no == 1: NO = 'no' elif no == 0: NO = '' if glob_del == 1: GLOB = 'no' else: GLOB = '' net_connect = ConnectHandler(**device) prompt = net_connect.find_prompt() host_no = re.findall('\d+', prompt)[0] hw_cli = [] data = net_connect.send_config_set('show mod') model = re.findall('N9K-C9372PX', data) if model: hw_cli += [ 'hardware access-list tcam region span 0', 'hardware access-list tcam region ifacl 0', 'hardware access-list tcam region arp-ether 256 double-wide ' ] print 'Device {} : TOR Model: {} Need TCAM carving'.format( prompt[:-1], model[0]) reboot = 1 else: reboot = 0 ##### global_feature = [ 'feature lldp', 'feature ospf', 'feature pim', 'feature bgp', 'feature lacp', 'feature bfd', 'feature pbr', 'feature interface-vlan' ] # Vxlan Config spine1_lo0 = '1.101.1.1' spine1_lo1 = '1.101.102.1' spine2_lo0 = '1.102.2.1' spine2_lo1 = '1.101.102.1' bgp_as = '65001' remote_as = '65001' loop0_ip = '1.0.{}.1'.format(host_no) loop1_ip = '1.0.{}.2'.format(host_no) print '{} loopback0 ip: {}, loopback1 ip: {}'.format( prompt[:-1], loop0_ip, loop1_ip) ########### loopback0 = [ '{} int loopback 0'.format(GLOB), 'ip address {}/32'.format(loop0_ip), 'ip pim sparse-mode', 'ip router ospf 1 area 0.0.0.0', 'no shut' ] loopback1 = [ '{} int loopback 1'.format(GLOB), 'ip address {}/32'.format(loop1_ip), 'ip pim sparse-mode', 'ip router ospf 1 area 0.0.0.0', 'no shut' ] ###### ospf = ['router ospf 1', 'bfd'] vrf_set_1 = 20 vrf_set_2 = 200 vnid = 100000 vlanset1 = 101 vlanset2 = 200 mcast_grp1 = '239.1.1.1' mcast_grp2 = '239.2.1.1' ############### rp = [ '{} ip pim rp-address {} group-list 239.0.0.0/8'.format( NO, spine1_lo1) ] bgp = [ '{} router bgp {}'.format(GLOB, bgp_as), 'neighbor-down fib-accelerate', 'router-id {}'.format(loop0_ip), 'address-family ipv4 unicast', '{} neighbor {}'.format(NO, spine1_lo0), 'remote-as {}'.format(remote_as), 'update-source loopback0', 'address-family l2vpn evpn', 'send-community extended', '{} neighbor {}'.format(NO, spine2_lo0), 'remote-as {}'.format(remote_as), 'update-source loopback0', 'address-family l2vpn evpn', 'send-community extended' ] bgp_vrf = ['{} vrf T{}'.format(NO, x) for x in range(1, vrf_no + 1)] vrf_cfg = [] ####### nve_cli_g = [ '{} system nve peer-vni-counter'.format(GLOB), '{} int nve 1'.format(GLOB), 'no shut', 'host-reachability protocol bgp', 'source-interface loopback1', 'source-interface hold-down-time 400' ] for vrf in range(1, vrf_no + 1): temp = [ '{} vrf context T{}'.format(GLOB, vrf), '{} vni {}'.format(NO, vnid + vrf), ' address-family ipv4 unicast', 'route-target both auto', 'route-target both auto evpn', 'address-family ipv6 unicast', 'route-target both auto', 'route-target both auto evpn' ] vrf_cfg += temp nve_cli_g.append('{} member vni {} associate-vrf'.format( NO, vnid + vrf)) for vrf in bgp_vrf: bgp.append(vrf) bgp.append('address-family ipv4 unicast') bgp.append('advertise l2vpn evpn') vxlan_map1 = [] vxlan_map2 = [] ######## evpn1 = [] evpn2 = [] nve_cli = [] for vni in range(vlanset1, vlanset1 + 20): temp1 = [ 'int nve 1', '{} member vni {}'.format(NO, vni), 'suppress-arp', 'mcast-group {}'.format(mcast_grp1), 'mcast-group {}'.format(mcast_grp1) ] temp2 = ['{} vlan {}'.format(NO, vni), 'vn-segment {}'.format(vni)] temp3 = [ 'evpn', '{} vni {} l2'.format(NO, vni), 'rd auto', 'route-target import auto', 'route-target export auto' ] nve_cli.append(temp1) vxlan_map1.append(temp2) evpn1.append(temp3) mcast_grp1 = inc.IP_SUBNET(mcast_grp1, net_incr=0, host_incr=1) for vni in range(vlanset2, vlanset2 + 200): temp1 = [ 'int nve 1', '{} member vni {}'.format(NO, vni), 'suppress-arp', 'mcast-group {}'.format(mcast_grp2), 'mcast-group {}'.format(mcast_grp2) ] temp2 = ['{} vlan {}'.format(NO, vni), 'vn-segment {}'.format(vni)] temp3 = [ 'evpn', '{} vni {} l2'.format(NO, vni), 'rd auto', 'route-target import auto', 'route-target export auto' ] nve_cli.append(temp1) vxlan_map2.append(temp2) evpn2.append(temp3) mcast_grp2 = inc.IP_SUBNET(mcast_grp2, net_incr=0, host_incr=1) ##### vlan_cfg1 = ['vlan 101-120', 'vlan 1001'] vlan_cfg2 = ['vlan 1101-1300', 'vlan 2001'] vlan_int_config = [] for vlan in range(101, 121): temp = [ '{} int vlan {}'.format(NO, vlan), 'no shutdown', 'vrf member T1', 'ip address 7.{}.1.1/24'.format(vlan), 'ipv6 address 2001:7:{}::1/64'.format(vlan), 'fabric forwarding mode anycast-gateway', 'no ip redirects', 'no ipv6 redirects', 'mtu 9216' ] vlan_int_config.append(temp) vlan_temp = 101 for vlan in range(1101, 1301): temp = [ '{} int vlan {}'.format(NO, vlan), 'no shutdown', 'vrf member T2', 'ip address 9.{}.1.1/24'.format(vlan_temp), 'ipv6 address 2001:9:{}::1/64'.format(vlan), 'fabric forwarding mode anycast-gateway', 'no ip redirects', 'no ipv6 redirects', 'mtu 9216' ] vlan_int_config.append(temp) vlan_temp += 1 temp = [ '{} int vlan 1001'.format(NO), 'no shutdown', 'vrf member T1', 'mtu 9216', '{} int vlan 2001'.format(NO), 'no shutdown', 'vrf member T2', 'mtu 9216' ] vlan_int_config.append(temp) ##### Applying Config output = net_connect.send_config_set(nve_cli_g, delay_factor=2) print '===> {}:\n {}'.format(prompt, output) for command in nve_cli: output = net_connect.send_config_set(command, delay_factor=2) print '===> {}:\n {}'.format(prompt, output) for command in vlan_int_config: output = net_connect.send_config_set(command, delay_factor=2) print '===> {}:\n {}'.format(prompt, output) output = net_connect.send_config_set('copy runn start') print output print 'Finshed config for device : {}'.format(prompt) if reboot == 1: output = net_connect.send_command('reload') print output output = net_connect.send_command('yes') print output net_connect.disconnect()
def CFG(ip, username='******', password='******', vrf_no=2, no=0, glob_del=0): device = { 'device_type': 'cisco_nxos', 'ip': ip, 'username': username, 'password': password } if no == 1: NO = 'no' elif no == 0: NO = '' if glob_del == 1: GLOB = 'no' else: GLOB = '' net_connect = ConnectHandler(**device) prompt = net_connect.find_prompt() host_no = re.findall('\d+', prompt)[0] hw_cli = [] data = net_connect.send_config_set('show mod') model = re.findall('N9K-C9372PX', data) global_feature = ['no router bgp 65001', 'no feature bgp'] spine1_lo0 = '3.101.1.1' spine2_lo0 = '3.102.1.1' spine_anycast = '3.101.102.1' bgp_as = '65001' remote_as = '65001' if int(host_no) < 255: loop0_ip = '1.0.{}.1'.format(host_no) loop1_ip = '2.0.{}.2'.format(host_no) elif int(host_no) > 254: h = int(host_no) hmod = str(h % 255) loop0_ip = '1.255.{}.1'.format(hmod) loop1_ip = '2.255.{}.2'.format(hmod) vrf_set_1 = 20 vrf_set_2 = 200 vnid = 100000 vlanset1 = 101 vlanset2 = 200 bgp = [ '{} router bgp {}'.format(GLOB, bgp_as), 'neighbor-down fib-accelerate', 'router-id {}'.format(loop0_ip), 'address-family ipv4 unicast', '{} neighbor {}'.format(NO, spine1_lo0), 'remote-as {}'.format(remote_as), 'update-source loopback0', 'address-family l2vpn evpn', 'send-community extended', '{} neighbor {}'.format(NO, spine2_lo0), 'remote-as {}'.format(remote_as), 'update-source loopback0', 'address-family l2vpn evpn', 'send-community extended' ] bgp_vrf = ['{} vrf T{}'.format(NO, x) for x in range(1, vrf_no + 1)] for vrf in bgp_vrf: bgp.append(vrf) bgp.append('address-family ipv4 unicast') bgp.append('advertise l2vpn evpn') ##### Applying Config for command in global_feature: command = command.strip() output = net_connect.send_config_set(command, delay_factor=2) print '===> {}:\n {}'.format(prompt, output) ERR(output, prompt, ip) output = net_connect.send_config_set('feature bgp') output = net_connect.send_config_set(bgp, delay_factor=2) print '===> {}:\n {}'.format(prompt, output) ERR(output, prompt, ip) output = net_connect.send_config_set('copy runn start') print output ERR(output, prompt, ip) print 'Finshed config for device : {}'.format(prompt) net_connect.disconnect()
class IOSDevice(BaseDevice): def __init__(self, host, username, password, secret='', port=22, **kwargs): super(IOSDevice, self).__init__(host, username, password, vendor='cisco', device_type='cisco_ios_ssh') self.native = None self.host = host self.username = username self.password = password self.secret = secret self.port = int(port) self.global_delay_factor = kwargs.get('global_delay_factor', 1) self.delay_factor = kwargs.get('delay_factor', 1) self._connected = False self.open() def _enable(self): self.native.exit_config_mode() if not self.native.check_enable_mode(): self.native.enable() def _enter_config(self): self._enable() self.native.config_mode() def _file_copy_instance(self, src, dest=None, file_system='flash:'): if dest is None: dest = os.path.basename(src) fc = FileTransfer(self.native, src, dest, file_system=file_system) return fc def _get_file_system(self): """Determines the default file system or directory for device. Returns: str: The name of the default file system or directory for the device. Raises: FileSystemNotFound: When the module is unable to determine the default file system. """ raw_data = self.show('dir') try: file_system = re.match(r'\s*.*?(\S+:)', raw_data).group(1) return file_system except AttributeError: raise FileSystemNotFoundError(hostname=self.facts.get("hostname"), command="dir") def _image_booted(self, image_name, **vendor_specifics): version_data = self.show("show version") if re.search(image_name, version_data): return True return False def _interfaces_detailed_list(self): ip_int_br_out = self.show('show ip int br') ip_int_br_data = get_structured_data( 'cisco_ios_show_ip_int_brief.template', ip_int_br_out) return ip_int_br_data def _is_catalyst(self): return self.facts['model'].startswith('WS-') def _raw_version_data(self): show_version_out = self.show('show version') try: version_data = get_structured_data( 'cisco_ios_show_version.template', show_version_out)[0] return version_data except IndexError: return {} def _send_command(self, command, expect=False, expect_string=''): if expect: if expect_string: response = self.native.send_command_expect( command, expect_string=expect_string) else: response = self.native.send_command_expect(command) else: response = self.native.send_command_timing(command) if '% ' in response or 'Error:' in response: raise CommandError(command, response) return response def _show_vlan(self): show_vlan_out = self.show('show vlan') show_vlan_data = get_structured_data('cisco_ios_show_vlan.template', show_vlan_out) return show_vlan_data def _uptime_components(self, uptime_full_string): match_days = re.search(r'(\d+) days?', uptime_full_string) match_hours = re.search(r'(\d+) hours?', uptime_full_string) match_minutes = re.search(r'(\d+) minutes?', uptime_full_string) days = int(match_days.group(1)) if match_days else 0 hours = int(match_hours.group(1)) if match_hours else 0 minutes = int(match_minutes.group(1)) if match_minutes else 0 return days, hours, minutes def _uptime_to_seconds(self, uptime_full_string): days, hours, minutes = self._uptime_components(uptime_full_string) seconds = days * 24 * 60 * 60 seconds += hours * 60 * 60 seconds += minutes * 60 return seconds def _uptime_to_string(self, uptime_full_string): days, hours, minutes = self._uptime_components(uptime_full_string) return '%02d:%02d:%02d:00' % (days, hours, minutes) def _wait_for_device_reboot(self, timeout=3600): start = time.time() while time.time() - start < timeout: try: self.open() return except: pass raise RebootTimeoutError(hostname=self.facts["hostname"], wait_time=timeout) def backup_running_config(self, filename): with open(filename, 'w') as f: f.write(self.running_config) def checkpoint(self, checkpoint_file): self.save(filename=checkpoint_file) def close(self): if self._connected: self.native.disconnect() self._connected = False def config(self, command): self._enter_config() self._send_command(command) self.native.exit_config_mode() def config_list(self, commands): self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode() @property def facts(self): if self._facts is None: version_data = self._raw_version_data() self._facts = convert_dict_by_key(version_data, ios_key_maps.BASIC_FACTS_KM) self._facts['vendor'] = self.vendor uptime_full_string = version_data['uptime'] self._facts['uptime'] = self._uptime_to_seconds(uptime_full_string) self._facts['uptime_string'] = self._uptime_to_string( uptime_full_string) self._facts['fqdn'] = 'N/A' self._facts['interfaces'] = list( x['intf'] for x in self._interfaces_detailed_list()) if self._facts['model'].startswith('WS'): self._facts['vlans'] = list( str(x['vlan_id']) for x in self._show_vlan()) else: self._facts['vlans'] = [] # ios-specific facts self._facts[self.device_type] = { 'config_register': version_data['config_register'] } return self._facts def file_copy(self, src, dest=None, file_system=None): self._enable() if file_system is None: file_system = self._get_file_system() if not self.file_copy_remote_exists(src, dest, file_system): fc = self._file_copy_instance(src, dest, file_system=file_system) # if not self.fc.verify_space_available(): # raise FileTransferError('Not enough space available.') try: fc.enable_scp() fc.establish_scp_conn() fc.transfer_file() except: raise FileTransferError finally: fc.close_scp_chan() if not self.file_copy_remote_exists(src, dest, file_system): raise FileTransferError( message="Attempted file copy, " "but could not validate file existed after transfer") # TODO: Make this an internal method since exposing file_copy should be sufficient def file_copy_remote_exists(self, src, dest=None, file_system=None): self._enable() if file_system is None: file_system = self._get_file_system() fc = self._file_copy_instance(src, dest, file_system=file_system) if fc.check_file_exists() and fc.compare_md5(): return True return False def get_boot_options(self): # TODO: CREATE A MOCK FOR TESTING THIS FUNCTION boot_path_regex = r"(?:BOOT variable\s+=\s+(\S+)\s*$|BOOT path-list\s+:\s*(\S+)\s*$)" try: # Try show bootvar command first show_boot_out = self.show('show bootvar') show_boot_out = show_boot_out.split( "Boot Variables on next reload", 1)[-1] except CommandError: try: # Try show boot if previous command was invalid show_boot_out = self.show('show boot') show_boot_out = show_boot_out.split( "Boot Variables on next reload", 1)[-1] except CommandError: # Default to running config value show_boot_out = self.show('show run | inc boot') boot_path_regex = r"boot\s+system\s+(?:\S+\s+|)(\S+)\s*$" match = re.search(boot_path_regex, show_boot_out, re.MULTILINE) if match: boot_path = match.group(1) file_system = self._get_file_system() boot_image = boot_path.replace(file_system, '') boot_image = boot_image.replace('/', '') boot_image = boot_image.split(',')[0] boot_image = boot_image.split(';')[0] else: boot_image = None return {'sys': boot_image} def open(self): if self._connected: try: self.native.find_prompt() except: self._connected = False if not self._connected: self.native = ConnectHandler( device_type='cisco_ios', ip=self.host, username=self.username, password=self.password, port=self.port, global_delay_factor=self.global_delay_factor, secret=self.secret, verbose=False) self._connected = True def reboot(self, timer=0, confirm=False): if confirm: def handler(signum, frame): raise RebootSignal('Interrupting after reload') signal.signal(signal.SIGALRM, handler) signal.alarm(10) try: if timer > 0: first_response = self.show('reload in %d' % timer) else: first_response = self.show('reload') if 'System configuration' in first_response: self.native.send_command_timing('no') self.native.send_command_timing('\n') except RebootSignal: signal.alarm(0) signal.alarm(0) else: print('Need to confirm reboot with confirm=True') def rollback(self, rollback_to): try: self.show('configure replace flash:%s force' % rollback_to) except CommandError: raise RollbackError('Rollback unsuccessful. %s may not exist.' % rollback_to) @property def running_config(self): return self.show('show running-config', expect=True) def save(self, filename='startup-config'): command = 'copy running-config %s' % filename # Changed to send_command_timing to not require a direct prompt return. self.native.send_command_timing(command) # If the user has enabled 'file prompt quiet' which dose not require any confirmation or feedback. # This will send return without requiring an OK. # Send a return to pass the [OK]? message - Incease delay_factor for looking for response. self.native.send_command_timing('\n', delay_factor=2) # Confirm that we have a valid prompt again before returning. self.native.find_prompt() return True def set_boot_options(self, image_name, **vendor_specifics): file_system = vendor_specifics.get("file_system") if file_system is None: file_system = self._get_file_system() file_system_files = self.show("dir {0}".format(file_system)) if re.search(image_name, file_system_files) is None: raise NTCFileNotFoundError(hostname=self.facts.get("hostname"), file=image_name, dir=file_system) try: command = "boot system {0}/{1}".format(file_system, image_name) self.config_list(['no boot system', command]) except CommandError: file_system = file_system.replace(':', '') command = "boot system {0} {1}".format(file_system, image_name) self.config_list(['no boot system', command]) self.save() new_boot_options = self.get_boot_options()["sys"] if new_boot_options != image_name: raise CommandError( command=command, message= "Setting boot command did not yield expected results, found {0}" .format(new_boot_options), ) def show(self, command, expect=False, expect_string=''): self._enable() return self._send_command(command, expect=expect, expect_string=expect_string) def show_list(self, commands): self._enable() responses = [] entered_commands = [] for command in commands: entered_commands.append(command) try: responses.append(self._send_command(command)) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) return responses @property def startup_config(self): return self.show('show startup-config')
- creates a dictionary named device to hold values of device - creates a login session for every device retrieved from the database - sends 'show ip interface b | ex ass|Interface', splits up output by horizontal line - lambda function splits up the ip_interface_split output by blank space, stores the 1st column as interface variable ''' for (hostname, device_vendor, device_type, mgmt_ip) in cursor: start_time = datetime.now() # logging timestamps device = {'device_type': 'cisco_ios', 'ip': mgmt_ip, 'username': username, 'password': password, 'verbose': False,} try: ssh_session = ConnectHandler(**device) hostname = ssh_session.find_prompt() # removes the # sign and stores as variable hostname hostname = hostname.replace("#", "") # some pretty printing to display the hostname print("=" * 64 + "\n" + 29 * " " + "% s \n" + "=" * 64) % hostname output = ssh_session.send_command('show ip interface b | ex ass|Interface') ip_interface_split = output.splitlines() ip_interface = map(lambda temp: temp.split()[0], ip_interface_split) ''' The following block of code handles the configuraiton compliance. - check for current PIM enabled interfaces with 'show run inter ' + interface + ' | include pim' - if the word 'pim' is found within show_run_interface_pim, then PIM has been configured - if 'pim' is not found, then enter the interface's configuration mode and properly update the configuration ''' for interface in ip_interface: try: show_run_interface_pim = ssh_session.send_command('show run inter ' + interface + ' | include pim') print "\nInterface {0} ".format(interface) # some console output to let us know which interface we are on
def healthcheck(sshdevice, usernamelist, exportlocation): # Definition Variables tempfilelist = [] healthchecklist = [] # Start sshdeviceip = sshdevice.get('Device IPs').encode('utf-8') sshdevicevendor = sshdevice.get('Vendor').encode('utf-8') sshdevicetype = sshdevice.get('Type').encode('utf-8') sshdevicetype = sshdevicevendor.lower() + "_" + sshdevicetype.lower() ### FSM Templates ### # FSM Show Interface if "cisco_ios" in sshdevicetype: templatename = "cisco_ios_show_interfaces_health.template" if "cisco_xe" in sshdevicetype: templatename = "cisco_ios_show_interfaces_health.template" if "cisco_nxos" in sshdevicetype: templatename = "cisco_nxos_show_interfaces_health.template" # Create template file path templatefile = os.path.join(templatepath, templatename) # Open and convert to TextFSM with open(templatefile, 'r') as fsmtemplatenamefile: fsminttemplate = textfsm.TextFSM(fsmtemplatenamefile) # FSM Show Temperature if "cisco_ios" in sshdevicetype: templatename = "cisco_ios_show_temp_health.template" if "cisco_xe" in sshdevicetype: templatename = "cisco_ios_show_temp_health.template" if "cisco_nxos" in sshdevicetype: templatename = "cisco_nxos_show_temp_health.template" # Create template file path templatefile = os.path.join(templatepath, templatename) # Open and store in memory with open(templatefile, 'r') as fsmtemplatenamefile: fsmtemptemplate = textfsm.TextFSM(fsmtemplatenamefile) #Start Connection try: for username in usernamelist: try: sshusername = username.get('sshusername').encode('utf-8') sshpassword = username.get('sshpassword').encode('utf-8') enablesecret = username.get('enablesecret').encode('utf-8') except: sshusername = username.get('sshusername') sshpassword = username.get('sshpassword') enablesecret = username.get('enablesecret') try: sshnet_connect = ConnectHandler(device_type=sshdevicetype, ip=sshdeviceip, username=sshusername, password=sshpassword, secret=enablesecret) break except Exception as e: if 'Authentication' in e: continue else: sshdevicetypetelnet = sshdevicetype + '_telnet' try: sshnet_connect = ConnectHandler( device_type=sshdevicetypetelnet, ip=sshdeviceip, username=sshusername, password=sshpassword, secret=enablesecret) break except: continue try: sshnet_connect except: print 'Error with connecting to ' + sshdeviceip sys.exit() sshdevicehostname = sshnet_connect.find_prompt() sshdevicehostname = sshdevicehostname.strip('#') if '>' in sshdevicehostname: sshnet_connect.enable() sshdevicehostname = sshdevicehostname.strip('>') sshdevicehostname = sshnet_connect.find_prompt() sshdevicehostname = sshdevicehostname.strip('#') print 'Health Check starting on ' + sshdevicehostname #Show Interfaces sshcommand = showinterface sshresult = sshnet_connect.send_command(sshcommand) hcshowint = fsminttemplate.ParseText(sshresult) #Parse through each interface looking for issues healthcheckcsv = [] for hcshowintsingle in hcshowint: hcinterfacename = hcshowintsingle[0].encode('utf-8') if not 'notconnect' in hcshowintsingle[2]: # Look for duplexing issues if 'Half-duplex' in hcshowintsingle[6]: hcerror = 'Duplex Mismatch' hcdescription = hcinterfacename + ' is showing as half-duplex. If this is by design please ignore.' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) if '10Mb/s' in hcshowintsingle[7]: hcerror = 'Duplex Mismatch' hcdescription = hcinterfacename + ' is showing as 10Mb/s. If this is by design please ignore.' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) # Look for interface counter errors # Input Errors hcshowintsingleint = hcshowintsingle[8] if hcshowintsingleint == '': hcshowintsingleint = 0 hcshowintsingleint = int(hcshowintsingleint) if hcshowintsingleint > 20: hcerror = 'Input Errors' hcinterfacecounter = hcshowintsingle[8] hcinterfacecounter = hcinterfacecounter.encode('utf-8') hcdescription = hcinterfacename + ' is showing ' + hcinterfacecounter + ' input errors. Usually indicative of a bad link (cabling and/or optic failure).' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) # CRC errors hcshowintsingleint = hcshowintsingle[9] if hcshowintsingleint == '': hcshowintsingleint = 0 hcshowintsingleint = int(hcshowintsingleint) if hcshowintsingleint > 20: hcerror = 'CRC Errors' hcinterfacecounter = hcshowintsingle[9] hcinterfacecounter = hcinterfacecounter hcinterfacecounter = hcinterfacecounter.encode('utf-8') hcdescription = hcinterfacename + ' is showing ' + hcinterfacecounter + ' CRC errors. Usually indicative of incorrect duplexing settings or a bad link (cabling and/or optic failure).' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) # Output errors hcshowintsingleint = hcshowintsingle[10] if hcshowintsingleint == '': hcshowintsingleint = 0 hcshowintsingleint = int(hcshowintsingleint) if hcshowintsingleint > 100: hcerror = 'Saturated Link' hcinterfacecounter = hcshowintsingle[10] hcinterfacecounter = hcinterfacecounter.encode('utf-8') hcdescription = hcinterfacename + ' is showing ' + hcinterfacecounter + ' output errors. This is usually indicative of a saturated interface. ' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) # Collisions hcshowintsingleint = hcshowintsingle[11] if hcshowintsingleint == '': hcshowintsingleint = 0 hcshowintsingleint = int(hcshowintsingleint) if hcshowintsingleint > 20: hcerror = 'Shared Medium' hcinterfacecounter = hcshowintsingle[11] hcinterfacecounter = hcinterfacecounter.encode('utf-8') hcdescription = hcinterfacename + ' is showing ' + hcinterfacecounter + ' collisions. ' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) # Interface resets hcshowintsingleint = hcshowintsingle[12] if hcshowintsingleint == '': hcshowintsingleint = 0 hcshowintsingleint = int(hcshowintsingleint) if hcshowintsingleint > 20: hcerror = 'Interface Reset Count' hcinterfacecounter = hcshowintsingle[12] hcinterfacecounter = hcinterfacecounter.encode('utf-8') hcdescription = hcinterfacename + ' is showing ' + hcinterfacecounter + ' interface resets. ' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) #Show Temperature try: if 'cisco_ios' in sshdevicetype.lower( ) or 'cisco_xe' in sshdevicetype.lower(): sshcommand = showtemp sshresult = sshnet_connect.send_command(sshcommand) hcshowtemp = fsmtemptemplate.ParseText(sshresult) hctempdegrees = hcshowtemp[0] hctempdegrees = hctempdegrees[0] hctempdegrees = hctempdegrees.encode('utf-8') hctempdegreesint = int(hctempdegrees) if hctempdegreesint > 45: hcerror = 'Temperature Alert' hcdescription = 'Temperature has been recorded at ' + hctempdegrees + ' Celsius. Please lower the temperature for the surrounding environment ' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) if 'cisco_nxos' in sshdevicetype.lower(): sshcommand = showtemp_nxos sshresult = sshnet_connect.send_command(sshcommand) hcshowtemp = fsmtemptemplate.ParseText(sshresult) hctempdegrees = hcshowtemp[0] hctempdegrees = hctempdegrees[0] hctempdegrees = hctempdegrees.encode('utf-8') hctempdegreesint = int(hctempdegrees) if hctempdegreesint > 45: hcerror = 'Temperature Alert' hcdescription = 'Temperature has been recorded at ' + hctempdegrees + ' Celsius. Please lower the temperature for the surrounding environment ' healthcheckcsv.append((sshdevicehostname + ',' + hcerror + ',' + hcdescription)) except: pass # Exit SSH sshnet_connect.disconnect() # Parse list into dictionary/list saveresultslistsplit = [] for saveresultsrow in healthcheckcsv: saveresultslistsplit.append(saveresultsrow.strip().split(',')) saveresultslistsplit = [ saveresultslistsplit[i:i + 3] for i in range(0, len(saveresultslistsplit), 3) ] for saveresultsplitrow in saveresultslistsplit: for saveresultssplitrow2 in saveresultsplitrow: tempdict = {} tempdict['Hostname'] = saveresultssplitrow2[:1][0] tempdict['Error'] = saveresultssplitrow2[1:][0] tempdict['Description'] = saveresultssplitrow2[2:][0] healthchecklist.append(tempdict) except IndexError: print 'Could not connect to device ' + sshdeviceip try: sshnet_connect.disconnect() except: pass except Exception as e: print 'Error while running health check with ' + sshdeviceip + '. Error is ' + str( e) try: sshnet_connect.disconnect() except: pass except KeyboardInterrupt: print 'CTRL-C pressed, exiting script' try: sshnet_connect.disconnect() except: pass print 'Completed health check for ' + sshdeviceip try: for file in tempfilelist: try: os.remove(file) except: pass except: pass return healthchecklist
class IOSXR(object): """ Establishes a connection with the IOS-XR device via SSH and facilitates the communication through the XML agent. """ _XML_SHELL = 'xml' _XML_MODE_PROMPT = r'XML>' _READ_DELAY = 0.1 # at least 0.1, corresponding to 600 max loops (60s timeout) _XML_MODE_DELAY = 1 # should be able to read within one second _ITERATOR_ID_ERROR_MSG = ( 'Non supported IteratorID in Response object.' 'Turn iteration off on your XML agent by configuring "xml agent [tty | ssl] iteration off".' 'For more information refer to' 'http://www.cisco.com/c/en/us/td/docs/ios_xr_sw/iosxr_r4-1/xml/programming/guide/xl41apidoc.pdf, 7-99.' 'Please turn iteration off for the XML agent.') def __init__(self, hostname, username, password, port=22, timeout=60, logfile=None, lock=True, **netmiko_kwargs): """ IOS-XR device constructor. :param hostname: (str) IP or FQDN of the target device :param username: (str) Username :param password: (str) Password :param port: (int) SSH Port (default: 22) :param timeout: (int) Timeout (default: 60 sec) :param logfile: File-like object to save device communication to or None to disable logging :param lock: (bool) Auto-lock config upon open() if set to True, connect without locking if False (default: True) :netmiko_kwargs (kwargs) Key-value args to forward to Netmiko. """ self.hostname = str(hostname) self.username = str(username) self.password = str(password) self.port = int(port) self.timeout = int(timeout) self.logfile = logfile self.lock_on_connect = lock self.locked = False self.netmiko_kwargs = netmiko_kwargs self._cli_prompt = None self._xml_agent_locker = Lock() self._xml_agent_alive = False def __getattr__(self, item): """ Dynamic getter to translate generic show commands. David came up with this dynamic method. It takes calls with show commands encoded in the name. I'll replace the underscores for spaces and issues the show command on the device... pretty neat! non keyword params for show command: all non keyword arguments is added to the command to allow dynamic parameters: eg: .show_interface("GigabitEthernet0/0/0/0") keyword params for show command: config=True/False : set True to run show command in config mode eg: .show_configuration_merge(config=True) """ def _getattr(*args, **kwargs): cmd = item.replace('_', ' ') for arg in args: cmd += " %s" % arg if kwargs.get("config"): response = self._execute_config_show(cmd) else: response = self._execute_show(cmd) match = re.search(".*(!! IOS XR Configuration.*)</Exec>", response, re.DOTALL) if match is not None: response = match.group(1) return response if item.startswith('show'): return _getattr else: raise AttributeError("type object '%s' has no attribute '%s'" % (self.__class__.__name__, item)) def make_rpc_call(self, rpc_command): """ Allow a user to query a device directly using XML-requests. :param rpc_command: (str) rpc command such as: <Get><Operational><LLDP><NodeTable></NodeTable></LLDP></Operational></Get> """ # ~~~ hack: ~~~ if not self.is_alive(): self.close() # force close for safety self.open() # reopen # ~~~ end hack ~~~ result = self._execute_rpc(rpc_command) return ET.tostring(result) def open(self): """ Open a connection to an IOS-XR device. Connects to the device using SSH and drops into XML mode. """ try: self.device = ConnectHandler(device_type='cisco_xr', ip=self.hostname, port=self.port, username=self.username, password=self.password, **self.netmiko_kwargs) self.device.timeout = self.timeout self._xml_agent_alive = True # successfully open thus alive except NetMikoTimeoutException as t_err: raise ConnectError(t_err.args[0]) except NetMikoAuthenticationException as au_err: raise ConnectError(au_err.args[0]) self._cli_prompt = self.device.find_prompt() # get the prompt self._enter_xml_mode() def is_alive(self): """ Returns the XML agent connection state (and SSH connection state). """ if hasattr(self.device, 'remote_conn'): return self.device.remote_conn.transport.is_active( ) and self._xml_agent_alive return False # remote_conn not there => connection not init => not alive def _timeout_exceeded(self, start=None, msg='Timeout exceeded!'): if not start: return False # reference not specified, noth to compare => no error if time.time() - start > self.timeout: # it timeout exceeded, throw TimeoutError raise TimeoutError(msg, self) return False def _lock_xml_agent(self, start=None): while (not self._xml_agent_locker.acquire(False) and not self._timeout_exceeded( start, 'Waiting to acquire the XML agent!')): # will wait here till the XML agent is ready to receive new requests # if stays too much, _timeout_exceeded will raise TimeoutError pass # do nothing, just wait return True # ready to go now def _unlock_xml_agent(self): if self._xml_agent_locker.locked(): self._xml_agent_locker.release() def _send_command_timing(self, command): return self.device.send_command_timing(command, delay_factor=self._READ_DELAY, max_loops=self._XML_MODE_DELAY / self._READ_DELAY, strip_prompt=False, strip_command=False) def _in_cli_mode(self): out = self._send_command_timing('\n') if not out: return False if self._cli_prompt in out: return True return False def _enter_xml_mode(self): self._unlock_xml_agent() # release - other commands should not have anyway access to the XML agent # when not in XML mode self._lock_xml_agent( ) # make sure it won't collide with other parallel requests out = self._send_command_timing( self._XML_SHELL) # send xml shell command if '0x24319600' in out: # XML agent is not enabled raise ConnectError( 'XML agent is not enabled. Please configure `xml agent tty iteration off`!', self) self._unlock_xml_agent() if self.lock_on_connect: self.lock() def _send_command(self, command, delay_factor=None, start=None, expect_string=None, read_output=None, receive=False): if not expect_string: expect_string = self._XML_MODE_PROMPT if read_output is None: read_output = '' if not delay_factor: delay_factor = self._READ_DELAY if not start: start = time.time() output = read_output last_read = '' if not read_output and not receive: # because the XML agent is able to process only one single request over the same SSH session at a time # first come first served self._lock_xml_agent(start) try: max_loops = self.timeout / delay_factor last_read = self.device.send_command_expect( command, expect_string=expect_string, strip_prompt=False, strip_command=False, delay_factor=delay_factor, max_loops=max_loops) output += last_read except IOError as ioe: if ((not last_read and self._in_cli_mode()) or (self._cli_prompt in output and "% Invalid input detected at '^' marker." in output)): # something happened # e.g. connection with the XML agent died while reading # netmiko throws error and the last output read is empty (ofc) # and in CLI mode # # OR # # Sometimes the XML agent simply exits and all issued commands provide the following output # (as in CLI mode) # <? # ^ # % Invalid input detected at '^' marker. # RP/0/RSP1/CPU0:edge01.dus01#<xml version="1.0" encoding="UTF-8"? # ^ # % Invalid input detected at '^' marker. # RP/0/RSP1/CPU0:edge01.dus01#<xml version # # Which of course does not contain the XML and netmiko throws the not found error # therefore we need to re-enter in XML mode self._enter_xml_mode() # and let's issue the command again if still got time if not self._timeout_exceeded(start=start): # if still got time # reiterate the command from the beginning return self._send_command(command, expect_string=expect_string, delay_factor=delay_factor) else: output += self._netmiko_recv() # try to read some more if '0xa3679e00' in output or '0xa367da00' in output: # when multiple parallel request are made, the device throws one of the the errors: # --- # ERROR: 0xa3679e00 'XML Service Library' detected the 'fatal' condition # 'Multiple concurrent requests are not allowed over the same session. # A request is already in progress on this session.' # # ERROR: 0xa367da00 XML Service Library' detected the 'fatal' condition # 'Sending multiple documents is not supported.' # --- # we could use a mechanism similar to NETCONF and push the requests in queue and serve them sequentially # BUT we are not able to assign unique IDs and identify the request-reply map # so will throw an error that does not help too much :( raise XMLCLIError('XML agent cannot process parallel requests!', self) if not output.strip().endswith('XML>'): if '0x44318c06' in output or (self._cli_prompt and expect_string != self._cli_prompt and \ (output.startswith(self._cli_prompt) or output.endswith(self._cli_prompt))): # sometimes the device throws a stupid error like: # ERROR: 0x44318c06 'XML-TTY' detected the 'warning' condition # 'A Light Weight Messaging library communication function returned an error': No such device or address # and the XML agent connection is closed, but the SSH connection is fortunately maintained # OR sometimes, the device simply exits from the XML mode without any clue # In both cases, we need to re-enter in XML mode... # so, whenever the CLI promt is detected, will re-enter in XML mode # unless the expected string is the prompt self._unlock_xml_agent() self._enter_xml_mode() # however, the command could not be executed properly, so we need to raise the XMLCLIError exception raise XMLCLIError( 'Could not properly execute the command. Re-entering XML mode...', self) if not output.strip( ): # empty output, means that the device did not start delivering the output # but for sure is still in XML mode as netmiko did not throw error if not self._timeout_exceeded(start=start): return self._send_command( command, receive=True, start=start) # let's try receiving more raise XMLCLIError(output.strip(), self) self._unlock_xml_agent() return str(output.replace('XML>', '').strip()) def _netmiko_recv(self): output = '' for tmp_output in self.device.receive_data_generator(): output += tmp_output return output # previous module function __execute_rpc__ def _execute_rpc(self, command_xml, delay_factor=.1): xml_rpc_command = '<?xml version="1.0" encoding="UTF-8"?><Request MajorVersion="1" MinorVersion="0">' \ + command_xml + '</Request>' response = self._send_command(xml_rpc_command, delay_factor=delay_factor) try: root = ET.fromstring(str.encode(response)) except ET.XMLSyntaxError as xml_err: if 'IteratorID="' in response: raise IteratorIDError(self._ITERATOR_ID_ERROR_MSG, self) raise InvalidXMLResponse( 'Unable to process the XML Response from the device!', self) if 'IteratorID' in root.attrib: raise IteratorIDError(self._ITERATOR_ID_ERROR_MSG, self) childs = [x.tag for x in list(root)] result_summary = root.find('ResultSummary') if result_summary is not None and int( result_summary.get('ErrorCount', 0)) > 0: if 'CLI' in childs: error_msg = root.find('CLI').get('ErrorMsg') or '' elif 'Commit' in childs: error_msg = root.find('Commit').get('ErrorMsg') or '' error_code = root.find('Commit').get('ErrorCode') or '' if error_code == '0x41866c00': # yet another pointless IOS-XR error: # if the config DB was changed by another process, # while the current SSH connection is established and alive, # we won't be able to commit and the device will throw the following error: # 'CfgMgr' detected the 'warning' condition # 'One or more commits have occurred from other configuration sessions since this session started # or since the last commit was made from this session.' # dumb. # in this case we need to re-open the connection with the XML agent _candidate_config = self.get_candidate_config(merge=True) self.discard_config() # discard candidate config try: # exiting from the XML mode self._send_command('exit', expect_string=self._cli_prompt) except XMLCLIError: pass # because does not end with `XML>` self._enter_xml_mode() # re-entering XML mode self.load_candidate_config(config=_candidate_config) return self.commit_config() elif error_code == '0x41864e00' or error_code == '0x43682c00': # raises this error when the commit buffer is empty raise CommitError( 'The target configuration buffer is empty.', self) else: error_msg = root.get('ErrorMsg') or '' error_msg += '\nOriginal call was: %s' % xml_rpc_command raise XMLCLIError(error_msg, self) if 'CLI' in childs: cli_childs = [x.tag for x in list(root.find('CLI'))] if 'Configuration' in cli_childs: output = root.find('CLI').find('Configuration').text elif 'Exec' in cli_childs: output = root.find('CLI').find('Exec').text if output is None: output = '' elif 'Invalid input detected' in output: raise InvalidInputError('Invalid input entered:\n%s' % output, self) return root # previous module function __execute_show__ def _execute_show(self, show_command): """ Executes an operational show-type command. """ rpc_command = '<CLI><Exec>{show_command}</Exec></CLI>'.format( show_command=escape_xml(show_command)) response = self._execute_rpc(rpc_command) raw_response = response.xpath('.//CLI/Exec')[0].text return raw_response.strip() if raw_response else '' # previous module function __execute_config_show__ def _execute_config_show(self, show_command, delay_factor=.1): """ Executes a configuration show-type command. """ rpc_command = '<CLI><Configuration>{show_command}</Configuration></CLI>'.format( show_command=escape_xml(show_command)) response = self._execute_rpc(rpc_command, delay_factor=delay_factor) raw_response = response.xpath('.//CLI/Configuration')[0].text return raw_response.strip() if raw_response else '' def close(self): """ Close the connection to the IOS-XR device. Clean up after you are done and explicitly close the router connection. """ if self.lock_on_connect or self.locked: self.unlock() # this refers to the config DB self._unlock_xml_agent() # this refers to the XML agent if hasattr(self.device, 'remote_conn'): self.device.remote_conn.close() # close the underlying SSH session def lock(self): """ Lock the config database. Use if Locking/Unlocking is not performaed automatically by lock=False """ if not self.locked: rpc_command = '<Lock/>' try: self._execute_rpc(rpc_command) except XMLCLIError: raise LockError('Unable to enter in configure exclusive mode!', self) self.locked = True def unlock(self): """ Unlock the IOS-XR device config. Use if Locking/Unlocking is not performaed automatically by lock=False """ if self.locked: rpc_command = '<Unlock/>' try: self._execute_rpc(rpc_command) except XMLCLIError: raise UnlockError('Unable to unlock the config!', self) self.locked = False def load_candidate_config(self, filename=None, config=None): """ Load candidate confguration. Populate the attribute candidate_config with the desired configuration and loads it into the router. You can populate it from a file or from a string. If you send both a filename and a string containing the configuration, the file takes precedence. :param filename: Path to the file containing the desired configuration. By default is None. :param config: String containing the desired configuration. """ configuration = '' if filename is None: configuration = config else: with open(filename) as f: configuration = f.read() rpc_command = '<CLI><Configuration>{configuration}</Configuration></CLI>'.format( configuration=escape_xml( configuration ) # need to escape, otherwise will try to load invalid XML ) try: self._execute_rpc(rpc_command) except InvalidInputError as e: self.discard_config() raise InvalidInputError(e.args[0], self) def get_candidate_config(self, merge=False, formal=False): """ Retrieve the configuration loaded as candidate config in your configuration session. :param merge: Merge candidate config with running config to return the complete configuration including all changed :param formal: Return configuration in IOS-XR formal config format """ command = "show configuration" if merge: command += " merge" if formal: command += " formal" response = self._execute_config_show(command) match = re.search(".*(!! IOS XR Configuration.*)$", response, re.DOTALL) if match is not None: response = match.group(1) return response def compare_config(self): """ Compare configuration to be merged with the one on the device. Compare executed candidate config with the running config and return a diff, assuming the loaded config will be merged with the existing one. :return: Config diff. """ _show_merge = self._execute_config_show('show configuration merge') _show_run = self._execute_config_show('show running-config') diff = difflib.unified_diff( _show_run.splitlines(1)[2:-2], _show_merge.splitlines(1)[2:-2]) return ''.join([x.replace('\r', '') for x in diff]) def compare_replace_config(self): """ Compare configuration to be replaced with the one on the device. Compare executed candidate config with the running config and return a diff, assuming the entire config will be replaced. :return: Config diff. """ diff = self._execute_config_show('show configuration changes diff') return ''.join(diff.splitlines(1)[2:-2]) def commit_config(self, label=None, comment=None, confirmed=None): """ Commit the candidate config. :param label: Commit comment, displayed in the commit entry on the device. :param comment: Commit label, displayed instead of the commit ID on the device. (Max 60 characters) :param confirmed: Commit with auto-rollback if new commit is not made in 30 to 300 sec """ rpc_command = '<Commit' if label: rpc_command += ' Label="%s"' % label if comment: rpc_command += ' Comment="%s"' % comment[:60] if confirmed: if 30 <= int(confirmed) <= 300: rpc_command += ' Confirmed="%d"' % int(confirmed) else: raise InvalidInputError( 'confirmed needs to be between 30 and 300 seconds', self) rpc_command += '/>' self._execute_rpc(rpc_command) def commit_replace_config(self, label=None, comment=None, confirmed=None): """ Commit the candidate config to the device, by replacing the existing one. :param comment: User comment saved on this commit on the device :param label: User label saved on this commit on the device :param confirmed: Commit with auto-rollback if new commit is not made in 30 to 300 sec """ rpc_command = '<Commit Replace="true"' if label: rpc_command += ' Label="%s"' % label if comment: rpc_command += ' Comment="%s"' % comment if confirmed: if 30 <= int(confirmed) <= 300: rpc_command += ' Confirmed="%d"' % int(confirmed) else: raise InvalidInputError( 'confirmed needs to be between 30 and 300 seconds', self) rpc_command += '/>' self._execute_rpc(rpc_command) def discard_config(self): """ Clear uncommited changes in the current session. Clear previously loaded configuration on the device without committing it. """ rpc_command = '<Clear/>' self._execute_rpc(rpc_command) def rollback(self, rb_id=1): """ Rollback the last committed configuration. :param rb_id: Rollback a specific number of steps. Default: 1 """ rpc_command = '<Unlock/><Rollback><Previous>{rb_id}</Previous></Rollback><Lock/>'.format( rb_id=rb_id) self._execute_rpc(rpc_command)