Next, we will discuss the content of yesterday. Because regular expressions are very powerful and complex, please refer to relevant documents for more information in this experiment. this tutorial only helps O & M engineers to get started quickly, if you do not understand it, you can use it directly as a tool. Next, we will discuss the content of yesterday. Because regular expressions are very powerful and complex, please refer to relevant documents for more information in this experiment. this tutorial only helps O & M engineers to get started quickly, if you do not understand it, you can use it directly as a tool.
Review the regular expression: DeviceName = re. search ('.*(? = # Show run) ', telreply). group (), telreply is the inspection content, so you can see it at the beginning of the log:
R1>enablePassword:R1#terminal length 0R1#show runBuilding configuration...
Obviously, this regular expression is used to match any character before '# show' (except for line breaks), so of course it is R1.
Perform a complex experiment to capture more configuration fragments, such as the following configuration, and find the IP address of each interface of the device:
############################!interface FastEthernet0/0ip address 192.168.1.200 255.255.255.0duplex half!interface GigabitEthernet1/0no ip addressshutdownnegotiation auto!interface Serial2/0ip address 192.168.2.200 255.255.255.0!############################
Assign it to log: (the real environment is to directly read log)
Log = ''' # There are three quotation marks. press enter to paste the configuration above, press enter to enclose the three quotation marks, and press enter. (Multiple strings can be pasted with three quotation marks ).
Check the log:
>>> log'\n!\ninterface FastEthernet0/0\n ip address 192.168.1.200 255.255.255.0\n duplex half\n!\ninterface GigabitEthernet1/0\n no ip address\n shutdown\n negotiation auto\n!\ninterface Serial2/0\n ip address 192.168.2.200 255.255.255.0\n!\n'>>>
We can find that all strings are in one line, and the line feed is actually '\ n'. if print (log) is used, the display effect is that there is a line feed above.
We can also find that all configurations of Cisco devices use exclamation points (!) Separated. each interface starts with 'interface', and the ip address is preceded by 'IP address '. It is hard to imagine that the regular expression contains 'interface (. *) \ n', which matches the first line of an interface configuration. The configuration end is '! ', So in '! 'Previous expressions require '(? = !) '. The number of rows in the intermediate configuration is not fixed. it can be determined that at least one row of configuration is available and there will be a line break, that is, '(. * \ n) + ?) '. '+? 'Indicates a non-greedy match with the previous expression (. * \ n) once or multiple times. Parentheses can achieve the capture effect, but if you want to turn off the capture effect, follow? : You can. This experiment is to match all strings, so we use're. findall ()'. I don't have much to explain. go directly to the code:
ljds = re.findall('interface(.*)\n((?:.*\n)+?)(?=!)',log)>>> ljds[(' FastEthernet0/0', ' ip address 192.168.1.200 255.255.255.0\n duplex half\n'), (' GigabitEthernet1/0', ' no ip address\n shutdown\n negotiation auto\n'), (' Serial2/0', ' ip address 192.168.2.200 255.255.255.0\n')]
The result is that each interface configuration is divided into interface name + configuration tuples and nested in the list.
Test complete! To obtain the IP address of each interface.
Now I have learned this method. in the future, I can use it to monitor the configuration or status of the device. for example, if the interface status is 'up', I will send an email to notify you.
The above is the content of the Python regular expression [2]. For more information, see The PHP Chinese website (www.php1.cn )!