-1

Fairly new to this world (network engineer by carrer).

I need to scrub the configuration files for some routers and need to guarantee that certain servers are configured, but no extra servers are there.

So, let's say I have three servers:

1.1.1.1, 1.1.1.2, 1.1.1.3

and the operator adds a fourth one (2.2.2.2, or any other ip address).

When scrubbing, I need to catch this fourth and signal so it can be marked for removal.

Well, I know how to match any ip address:

(25[0-5]|2[0-4][0-9]|[01]?[0-68-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

But how to select any IP address except the ones I need? I tried negative lookahead, but I either did not understand the use of it or used it wrong, because it did nothing to me.

Any hints?

  • What tool are you using? Some have built in negation. For example, if you're using `grep`, I find it's easiest to do `grep -e "pattern I want" | grep -v -e "pattern to exclude"` You can do it with regex itself, but it becomes cumbersome to write and maintain. I can offer a regex solution, as well – VLAZ Apr 02 '20 at 19:01
  • How do you "signal" anything with regex? Regex only matches text or not. What do you want to match? All that your long regex matches but... what? Or what is the logic you are after? – Wiktor Stribiżew Apr 02 '20 at 19:04
  • @VLAZ It's an in-house system, which I never looked into. But my guess is that it uses grep/egrep. – Marcelo Rosa Apr 03 '20 at 13:45
  • @Wiktor when it matches, the system will get that config line and mark it as bad, so it can appear in a report. Sorry for the confusion. – Marcelo Rosa Apr 03 '20 at 13:47
  • So, you want to match any IP but `2.2.2.2`? Is the IP the only "word" in the string? – Wiktor Stribiżew Apr 03 '20 at 13:48

2 Answers2

0

I'm not sure what you're writing in, but I would do that on the return.

First pull all the ip addresses you find with your regex (which looks fine at a glance) then just not return anything if it matches one you want to ignore.

xartle
  • 9
  • 3
0

I think this answer will help you solve your problem: Regex not operator You should use as many lookaheads as you have valid IP addresses.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Well, I kinda found it when I took my head out of my ***. I used Negative Lookaheand and made this: ```(?!1\.1\.1\.[1-3]).*$ Just don't know about performance, but it is not an issue right now. I'll mark your answer as correct, because actually it is the same solution I came up with. Thanks everyone for the help! – Marcelo Rosa Apr 03 '20 at 13:52