In general, there is a configuration file, config_sw1.txt, with lines in it. There is a list of ignore with words. The task is to read lines from a file, output only those lines that do not begin with '!' and in which there are no words from the ignore list.
Code:
ignore = ['duplex', 'alias', 'Current configuration'] currentlist = [] with open('config_sw1.txt', 'r') as f: for line in f: if line[0] != '!' and line[0] != '\n': currentlist.append(line) for line in currentlist: for item in ignore: if item in line: currentlist.remove(line) for line in currentlist: print(line) It seems like it even works, but for some reason some of the lines are not deleted. There are about 6 lines left, starting with 'alias'.
Contents of the file 'config_sw1.txt':
Current configuration : 2033 bytes ! ! Last configuration change at 13:11:59 UTC Thu Feb 25 2016 ! version 15.0 service timestamps debug datetime msec service timestamps log datetime msec no service password-encryption ! hostname sw1 ! ! ! ! ! ! ! ! ! ! ! interface Ethernet0/0 duplex auto ! ! ! ! ! ! ! ! interface Ethernet0/1 switchport trunk encapsulation dot1q switchport trunk allowed vlan 100 switchport mode trunk duplex auto spanning-tree portfast edge trunk ! interface Ethernet0/2 duplex auto ! interface Ethernet0/3 switchport trunk encapsulation dot1q switchport trunk allowed vlan 100 duplex auto switchport mode trunk spanning-tree portfast edge trunk ! interface Ethernet1/0 duplex auto ! interface Ethernet1/1 duplex auto ! interface Ethernet1/2 duplex auto ! interface Ethernet1/3 duplex auto ! interface Vlan100 ip address 10.0.100.1 255.255.255.0 ! ! alias configure sh do sh alias exec ospf sh run | s ^router ospf alias exec bri show ip int bri | exc unass alias exec id show int desc alias exec top sh proc cpu sorted | excl 0.00%__0.00%__0.00% alias exec c conf t alias exec diff sh archive config differences nvram:startup-config system:running-config alias exec shcr sh run | s ^crypto alias exec desc sh int desc | ex down alias exec bgp sh run | s ^router bgp alias exec xc sh xconnect all alias exec vc sh mpls l2tr vc ! line con 0 exec-timeout 0 0 privilege level 15 logging synchronous line aux 0 line vty 0 4 login transport input all ! end Python script output:
version 15.0 service timestamps debug datetime msec service timestamps log datetime msec no service password-encryption hostname sw1 interface Ethernet0/0 interface Ethernet0/1 switchport trunk encapsulation dot1q switchport trunk allowed vlan 100 switchport mode trunk spanning-tree portfast edge trunk interface Ethernet0/2 interface Ethernet0/3 switchport trunk encapsulation dot1q switchport trunk allowed vlan 100 switchport mode trunk spanning-tree portfast edge trunk interface Ethernet1/0 interface Ethernet1/1 interface Ethernet1/2 interface Ethernet1/3 interface Vlan100 ip address 10.0.100.1 255.255.255.0 alias exec ospf sh run | s ^router ospf alias exec id show int desc alias exec c conf t alias exec shcr sh run | s ^crypto alias exec bgp sh run | s ^router bgp alias exec vc sh mpls l2tr vc line con 0 exec-timeout 0 0 privilege level 15 logging synchronous line aux 0 line vty 0 4 login transport input all end