Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ciscoconfparse import CiscoConfParse
- import netmiko
- import getpass
- def standardize_intfs(parse):
- for intf in parse.find_objects(r'^interface'):
- is_switchport_vlan_999 = intf.has_child_with(r'switchport access vlan 999')
- is_switchport_access = intf.has_child_with(r'switchport mode access')
- is_switchport_trunk = intf.has_child_with(r'switchport mode trunk')
- is_filter_enable = intf.has_child_with(r'spanning-tree bpdufilter enable')
- is_host_port_ = intf.has_child_with(r'spanning-tree portfast')
- has_dot1q = intf.has_child_with(r'switchport trunk encapsulation dot1q')
- has_trunk_vlan = intf.has_child_with(r'switchport trunk allowed vlan 999')
- #Check if a port needs to be configured as a trunk
- if is_switchport_vlan_999 and (not is_switchport_trunk) and (not is_host_port_):
- if not (has_trunk_vlan):
- intf.append_to_family(' switchport trunk allowed vlan 999')
- if has_dot1q:
- intf.append_to_family(' switchport mode trunk')
- print("Trunk configuration added on interface")
- else:
- intf.append_to_family(' switchport trunk encapsulation dot1q')
- intf.append_to_family(' switchport mode trunk')
- print("Trunk configuration added on interface")
- if is_switchport_access:
- intf.delete_children_matching('switchport mode access')
- print("Access Port configuration deleted")
- else:
- print("Port is not in access mode")
- #Check if bpdufilter is enabled
- if is_filter_enable:
- intf.delete_children_matching('spanning-tree bpdufilter enable')
- print("Deleted bpdufilter")
- else:
- print("No bpdufilter to delete")
- # grab some info from the user
- ip = raw_input('Device IP: ')
- un = raw_input('Username: ')
- pw = getpass.getpass()
- # define your device
- cisco_ios = {
- 'device_type': 'cisco_ios',
- 'ip': ip,
- 'username': un,
- 'password': pw,
- 'secret': pw,
- }
- # connect to the device w/ netmiko
- net_connect = netmiko.ConnectHandler(**cisco_ios)
- net_connect.enable()
- net_connect.send_command('show run | i hostname')
- # get the prompt as a string
- prompt = net_connect.find_prompt()
- hostname = net_connect.send_command('show run | i hostname')
- hostname.split(" ")
- col1,col2 = hostname.split(None, 1)
- print "Working on " + col2
- filename = col2
- showrun = net_connect.send_command('term len 0')
- showrun = net_connect.send_command('show run | beg interface')
- log_file = open(filename, 'w') # in append mode
- log_file.write(showrun)
- new_path = ('C:\Users\\username\\some_directory\\' + filename)
- parse = CiscoConfParse(new_path)
- standardize_intfs(parse)
- parse.commit()
- parse.save_as(new_path)
- log_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement