adamZ88 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am working with SNMP and perl. I am walking oid ipNetToMediaPhysAddress oid. The results always vary, but follow the following format nn.ooo.ooo.ooo.ooo:mmm. Here is an actual example of the result of walking that oid: 258.10.73.138.11:00 00 00 00 00 00. nn=is a variable length numeric string oo is an octet(decimal from 0 - 255) mm is a hex string

I want to compare a string containing an ip address to the results of this walk for an exact match to the "oo" values, I do not care about the rest of the values. For, example I want to check if "10.73.138.11" is equal to 258.10.73.138.11:00 00 00 00 00 00. I guess a different way to explain it is I want to check if the result contains the exact IP address that I need to inquire about. Here is my code so far:

(@arpResults) = &snmpwalk("$secret\@$gw", "$ipNetToMediaPhysAddress"); foreach $linebuf (@arpResults) { if ($linebuf =~ /\d+\.$oc1\.$oc2\.$oc3\.$oc4\:*/){ print "$linebuf\n"; my $val; ($arpMib, $arpOctetstr) = split(/:/, $linebuf, 2); ($distIndex) = split (/\./,$arpMib, 2); map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $arpOctetstr; $macFromARP= uc (join(":",unpack("a2 a2 a2 a2 a2 a2",$val))); ($ifNamez)=&snmpget("$secret\@$gw", "$ifName\.$distIndex"); $vlan = $ifNamez; $vlan =~ s/\D//g; print "$arpMib; $distIndex:$ifNamez:-$vlan Value=\t$macFromARP\n +"; }}

This is not working out for me because it still matches ips that have a forth octet begining with "11". Help is greatly appreciated.

  • Comment on Regex to match an IP address that is embedded inside of another string
  • Download Code

Replies are listed 'Best First'.
Re: Regex to match an IP address that is embedded inside of another string
by stevieb (Canon) on Apr 19, 2016 at 22:04 UTC

    Is the colon always going to be in place after the last octet? With /\d+\.$oc1\.$oc2\.$oc3\.$oc4\:*/, first, you don't need to escape the colon, and second, you use *, so that means 0 or more. That means that it'll ignore the colon if it means a longer match. The following will match the 4th octet exactly before the colon, or it won't match at all:

    /\d+\.$oc1\.$oc2\.$oc3\.$oc4:/
Re: Regex to match an IP address that is embedded inside of another string
by GrandFather (Saint) on Apr 19, 2016 at 22:05 UTC

      Thank you for your prompt response. The colon and other hex characters will always follow the last octet. I Tried your suggestion, and it still matches other ips that begin the same way on the fourth octet. Here is how I modified my code

      if ($linebuf =~ /\d+\.$oc1\.$oc2\.$oc3\.$oc4:*/

        As I stated in my above post, you need to get rid of the *.

      Will do, Thank you.

Re: Regex to match an IP address that is embedded inside of another string
by Anonymous Monk on Apr 19, 2016 at 22:11 UTC
    #!/usr/bin/perl use strict; use warnings; print my $lookfor = '10.73.138.11', "\n\n"; print /\b\Q$lookfor\E\b/ ? "matches $_\n" : "fails $_\n" for qw( 110.73.138.11 10.73.138.11 10.73.138.111 10.735138.11 );