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.


In reply to Regex to match an IP address that is embedded inside of another string by adamZ88

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.