Advertisement
Guest User

config change code

a guest
Oct 15th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. from ciscoconfparse import CiscoConfParse
  2. import netmiko
  3. import getpass
  4.  
  5. def standardize_intfs(parse):
  6.     for intf in parse.find_objects(r'^interface'):
  7.         is_switchport_vlan_999 = intf.has_child_with(r'switchport access vlan 999')
  8.         is_switchport_access = intf.has_child_with(r'switchport mode access')
  9.         is_switchport_trunk = intf.has_child_with(r'switchport mode trunk')
  10.         is_filter_enable = intf.has_child_with(r'spanning-tree bpdufilter enable')
  11.         is_host_port_ = intf.has_child_with(r'spanning-tree portfast')
  12.         has_dot1q = intf.has_child_with(r'switchport trunk encapsulation dot1q')
  13.         has_trunk_vlan = intf.has_child_with(r'switchport trunk allowed vlan 999')
  14.  
  15.         #Check if a port needs to be configured as a trunk
  16.         if is_switchport_vlan_999 and (not is_switchport_trunk) and (not is_host_port_):      
  17.             if not (has_trunk_vlan):
  18.                 intf.append_to_family(' switchport trunk allowed vlan 999')  
  19.             if has_dot1q:            
  20.                 intf.append_to_family(' switchport mode trunk')
  21.                 print("Trunk configuration added on interface")
  22.             else:
  23.                 intf.append_to_family(' switchport trunk encapsulation dot1q')
  24.                 intf.append_to_family(' switchport mode trunk')
  25.                 print("Trunk configuration added on interface")
  26.             if is_switchport_access:
  27.                 intf.delete_children_matching('switchport mode access')
  28.                 print("Access Port configuration deleted")
  29.             else:                                        
  30.                 print("Port is not in access mode")            
  31.        
  32.         #Check if bpdufilter is enabled
  33.         if is_filter_enable:
  34.             intf.delete_children_matching('spanning-tree bpdufilter enable')
  35.             print("Deleted bpdufilter")
  36.         else:
  37.             print("No bpdufilter to delete")
  38.  
  39. # grab some info from the user        
  40. ip = raw_input('Device IP: ')
  41. un = raw_input('Username: ')
  42. pw = getpass.getpass()
  43.  
  44.  
  45. # define your device
  46. cisco_ios = {
  47.     'device_type': 'cisco_ios',
  48.     'ip': ip,
  49.     'username': un,
  50.     'password': pw,
  51.     'secret': pw,
  52. }
  53.  
  54. # connect to the device w/ netmiko
  55. net_connect = netmiko.ConnectHandler(**cisco_ios)
  56.  
  57. net_connect.enable()
  58.  
  59. net_connect.send_command('show run | i hostname')
  60.  
  61.  
  62. # get the prompt as a string
  63. prompt = net_connect.find_prompt()
  64.  
  65. hostname = net_connect.send_command('show run | i hostname')
  66. hostname.split(" ")
  67. col1,col2 = hostname.split(None, 1)
  68. print "Working on " + col2
  69. filename = col2
  70.  
  71. showrun = net_connect.send_command('term len 0')
  72. showrun = net_connect.send_command('show run | beg interface')
  73.  
  74. log_file = open(filename, 'w') # in append mode
  75. log_file.write(showrun)
  76.  
  77.  
  78. new_path = ('C:\Users\\username\\some_directory\\' + filename)
  79. parse = CiscoConfParse(new_path)
  80. standardize_intfs(parse)
  81. parse.commit()
  82. parse.save_as(new_path)
  83. log_file.close()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement