I'm trying to convert the cisco running configuration into parameters using python, and i'm stuck at reading configuration sections with python. Say for instance you have the below stanza:
!
interface Async1
no ip address
encapsulation slip
!
router bgp 65500
bgp router-id 1.1.1.1
bgp log-neighbor-changes
timers bgp 10 30
neighbor 1.2.3.4 remote-as 1234
neighbor 1.2.3.4 description Some Description
neighbor 1.2.3.4 update-source GigabitEthernet0/0
!
address-family ipv4
network 2.2.2.2 mask 255.255.255.255
network 3.3.3.0 mask 255.255.255.252
neighbor 1.2.3.4 activate
neighbor 1.2.3.4 allowas-in 3
neighbor 1.2.3.4 prefix-list PXL out
exit-address-family
!
ip forward-protocol nd
no ip http server
no ip http secure-server
!
I want to read lines from 'router bgp' until first line that starts with '!' (e.g ^!), and then re-read the block to extract parameters into variables. A sample output would be:
router bgp 65500
bgp router-id 1.1.1.1
bgp log-neighbor-changes
timers bgp 10 30
neighbor 1.2.3.4 remote-as 1234
neighbor 1.2.3.4 description Some Description
neighbor 1.2.3.4 update-source GigabitEthernet0/0
!
address-family ipv4
network 2.2.2.2 mask 255.255.255.255
network 3.3.3.0 mask 255.255.255.252
neighbor 1.2.3.4 activate
neighbor 1.2.3.4 allowas-in 3
neighbor 1.2.3.4 prefix-list PXL out
exit-address-family
!
Note: I am able to extract the above code using awk or grep, but I want to translate my bash code to Python.
Thanks!