I am searching in a series of large Redback config files for certain things, and I’m beginning to find Regex and Atom really powerful for this. The files are sometimes 20,000 lines long, and there are over 100 of them.
Of course I should script this, and someone more script savvy than me would do that in a trice, but I’ve come up with a part manual solution. Perhaps I will build it into a script later.
What I need to do is search each file for any ‘ip route’ commands that have a named interface as a next-hop rather than an IP address. So to do this, I am doing inverse-matching on four sets of numbers separated by dots.
I also need to exclude the keyword ‘context’ and the interface ‘null0’. This took me a while to figure out.
Here’s my pattern match:
ip route [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+ (?![0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|context|null0)
This matches the string:
ip route 172.21.0.0/16 MADEUPINTERFACE
ip route 172.16.4.0/24 10.0.0.1
The expression is not very accurate, since it could match IP addresses like 999.999.999.999, but that does not matter in this case. Since the config came from a router, the router’s CLI has already vetted for illegal IP addresses anyway.
When you do this in Atom’s Regex find box it becomes even more useful. As you can see below, I’ve got the regex in the search box. Atom colours things for you to tell you when you’ve done something wrong in the expression.
Once you’ve got your expression right, you simply have to click down the other files in the same directory (shown in the sidebar) to easily apply the same regex to those. A massive timesaver.