[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3007
  • Last Modified:

Match IP address to Mac Address

In this question: http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21846607.html I asked for a script that helped me match mac address on a cisco catalyst switch to it's switch port. I'd now like to match the same mac address's to their ip address.

The output of the mac-to-sw_port looks like this:
Mac address= 00 05 5B 8E 5C 40  port=5/29
Mac address= 00 05 5B 38 66 0C  port=1/1
Mac address= 00 06 5B 8E 14 17  port=7/24
Mac address= 00 C0 D0 EA 36 49  port=4/12
Mac address= 00 C0 D0 49 8B AD  port=1/1

The data for the mac to ip looks like this:
.1.3.6.1.2.1.4.22.1.2.23.10.0.151.14 = STRING: 0:5:5b:8e:5c:40
.1.3.6.1.2.1.4.22.1.2.23.10.1.11.240 = STRING: 0:5:5b:38:66:0c
.1.3.6.1.2.1.4.22.1.2.23.10.1.11.254 = STRING: 0:6:5b:8e:14:17
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.202 = STRING: 0:c0:d0:ea:36:49
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.206 = STRING: 0:c0:d0:49:8b:ad

This portion can be ignored/chomped .1.3.6.1.2.1.4.22.1.2.24.
The ip address follows the chomped portion. Also note that LEADING 0's are dropped, so the mac addres of  "0:6:5b:8e:14:17" is actually 00:06:5b:8e:14:17

I'm not sure if it's possible or easier  to use the previous script linked above, to add a line that looks for "file5" that would contain the ip to mac strings above, and output like this:
Mac address= 00 C0 D0 EA 36 49  port=4/12 IP= 10.0.25.202
Mac address= 00 05 5B 38 66 0C  port=1/1 IP = 10.1.11.240

-rich


0
Rich Rumble
Asked:
Rich Rumble
  • 4
1 Solution
 
ps15Commented:
As an addition to the other script:


#!/usr/bin/perl

open(IN,"<file1.txt") or die "Could not open file1: $!\n";
while(<IN>){
     #   .1.3.6.1.2.1.17.4.3.1.1.0.0.12.7.172.8 = Hex-STRING: 00 00 0C 07 AC 08
     if(/.1.3.6.1.2.1.17.4.3.1.1.([\d\.]+)\s*=\s*Hex-STRING: (.*)/){
          $File1{$1}=$2;
     }
}
close(IN);


open(IN,"<file2.txt") or die "Could not open file2: $!\n";
while(<IN>){
     #   .1.3.6.1.2.1.17.4.3.1.2.0.0.12.7.172.8 = INTEGER: 1
     if(/.1.3.6.1.2.1.17.4.3.1.2.([\d\.]+)\s*=\s*INTEGER:\s*(\d+)/){
          $File2{$1}=$2;
     }
}
close(IN);

open(IN,"<file3.txt") or die "Could not open file3: $!\n";
while(<IN>){
     #   .1.3.6.1.2.1.17.1.4.1.2.1 = INTEGER: 422
     if(/.1.3.6.1.2.1.17.1.4.1.2.([\d]+)\s*=\s*INTEGER:\s*(\d+)/){
          $File3{$1}=$2;
     }
}
close(IN);

open(IN,"<file4.txt") or die "Could not open file4: $!\n";
while(<IN>){
     #   .1.3.6.1.2.1.31.1.1.1.1.422 = STRING: 1/1
     if(/.1.3.6.1.2.1.31.1.1.1.1.([\d]+)\s*=\s*STRING:\s*(.+)/){
          $File4{$1}=$2;
     }
}
close(IN);

open(IN,"<file5.txt") or die "Could not open file5: $!\n";
while(<IN>){
     # .1.3.6.1.2.1.4.22.1.2.24.10.0.25.202 = STRING: 0:c0:d0:ea:36:49
      if (/(\d+\.\d+\.\d+\.\d+)\s+=\s+STRING:\s+([0-9a-z]+):([0-9a-z]+):([0-9a-z]+):([0-9a-z]+):([0-9a-z]+):([0-9a-z]+)/)  {
            $File5{join " ", map {(length > 1)?uc $_:"0".uc $_} ($2,$3,$4,$5,$6,$7)} = $1;
      }
}
close(IN);

foreach $F1 (keys %File1){
     print "Mac address=$File1{$F1} port=" . $File4{$File3{$File2{$F1}}} . " IP = ". $File5{$File1{$F1}} ."\n";
}
0
 
Rich RumbleSecurity SamuraiAuthor Commented:
Thanks for the quick reply, however I'm not seeing the IP being printed on the output, here is what I have:
 cat file1.txt
.1.3.6.1.2.1.17.4.3.1.1.0.0.12.7.172.8 = Hex-STRING: 00 00 0C 07 AC 08
.1.3.6.1.2.1.17.4.3.1.1.0.3.50.168.20.0 = Hex-STRING: 00 03 32 A8 14 00
.1.3.6.1.2.1.17.4.3.1.1.0.6.91.56.114.187 = Hex-STRING: 00 06 5B 38 72 BB
.1.3.6.1.2.1.17.4.3.1.1.0.176.208.73.32.202 = Hex-STRING: 00 B0 D0 49 20 CA
.1.3.6.1.2.1.17.4.3.1.1.0.176.208.73.68.99 = Hex-STRING: 00 B0 D0 49 44 63
.1.3.6.1.2.1.17.4.3.1.1.0.176.208.73.68.130 = Hex-STRING: 00 B0 D0 49 44 82
.1.3.6.1.2.1.17.4.3.1.1.0.176.208.73.68.228 = Hex-STRING: 00 B0 D0 49 44 E4
.1.3.6.1.2.1.17.4.3.1.1.0.176.208.73.139.137 = Hex-STRING: 00 B0 D0 49 8B 89
.1.3.6.1.2.1.17.4.3.1.1.0.208.183.142.235.150 = Hex-STRING: 00 D0 B7 8E EB 96
.1.3.6.1.2.1.17.4.3.1.1.0.224.129.43.118.231 = Hex-STRING: 00 E0 81 2B 76 E7

 cat file2.txt
.1.3.6.1.2.1.17.4.3.1.2.0.0.12.7.172.8 = INTEGER: 1
.1.3.6.1.2.1.17.4.3.1.2.0.3.50.168.20.0 = INTEGER: 152
.1.3.6.1.2.1.17.4.3.1.2.0.6.91.56.114.187 = INTEGER: 361
.1.3.6.1.2.1.17.4.3.1.2.0.176.208.73.32.202 = INTEGER: 262
.1.3.6.1.2.1.17.4.3.1.2.0.176.208.73.68.99 = INTEGER: 261
.1.3.6.1.2.1.17.4.3.1.2.0.176.208.73.68.130 = INTEGER: 259
.1.3.6.1.2.1.17.4.3.1.2.0.176.208.73.68.228 = INTEGER: 357
.1.3.6.1.2.1.17.4.3.1.2.0.176.208.73.139.137 = INTEGER: 1
.1.3.6.1.2.1.17.4.3.1.2.0.208.183.142.235.150 = INTEGER: 1
.1.3.6.1.2.1.17.4.3.1.2.0.224.129.43.118.231 = INTEGER: 1

 cat file3.txt
.1.3.6.1.2.1.17.1.4.1.2.1 = INTEGER: 422
.1.3.6.1.2.1.17.1.4.1.2.152 = INTEGER: 240
.1.3.6.1.2.1.17.1.4.1.2.259 = INTEGER: 278
.1.3.6.1.2.1.17.1.4.1.2.261 = INTEGER: 280
.1.3.6.1.2.1.17.1.4.1.2.262 = INTEGER: 281
.1.3.6.1.2.1.17.1.4.1.2.357 = INTEGER: 408
.1.3.6.1.2.1.17.1.4.1.2.361 = INTEGER: 412

 cat file4.txt
.1.3.6.1.2.1.31.1.1.1.1.422 = STRING: 1/1
.1.3.6.1.2.1.31.1.1.1.1.240 = STRING: 3/24
.1.3.6.1.2.1.31.1.1.1.1.278 = STRING: 5/3
.1.3.6.1.2.1.31.1.1.1.1.280 = STRING: 5/5
.1.3.6.1.2.1.31.1.1.1.1.281 = STRING: 5/6
.1.3.6.1.2.1.31.1.1.1.1.408 = STRING: 6/37
.1.3.6.1.2.1.31.1.1.1.1.412 = STRING: 6/41

cat file5.txt
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.6 = STRING: 0:0:C:7:AC:8
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.26 = STRING: 0:3:32:A8:14:0
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.206 = STRING: 0:6:5B:38:72:BB
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.16 = STRING: 0:B0:D0:49:20:CA
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.20 = STRING: 0:B0:D0:49:44:63
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.246 = STRING: 0:B0:D0:49:44:82
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.216 = STRING: 0:B0:D0:49:44:E4
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.208 = STRING: 0:B0:D0:49:8B:89
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.209 = STRING: 0:D0:B7:8E:EB:96
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.209 = STRING: 0:E0:81:2B:76:E7

Output:
 ./ip-mac-sp.pl
Mac address=00 00 0C 07 AC 08 port=1/1 IP =
Mac address=00 B0 D0 49 44 63 port=5/5 IP =
Mac address=00 03 32 A8 14 00 port=3/24 IP =
Mac address=00 B0 D0 49 8B 89 port=1/1 IP =
Mac address=00 B0 D0 49 20 CA port=5/6 IP =
Mac address=00 B0 D0 49 44 82 port=5/3 IP =
Mac address=00 E0 81 2B 76 E7 port=1/1 IP =
Mac address=00 B0 D0 49 44 E4 port=6/37 IP =
Mac address=00 06 5B 38 72 BB port=6/41 IP =
Mac address=00 D0 B7 8E EB 96 port=1/1 IP =

Ideas? Thanks!
-rich
0
 
Rich RumbleSecurity SamuraiAuthor Commented:
the case's in file5.txt are actually lowercase...
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.6 = STRING: 0:0:C:7:ac:8
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.26 = STRING: 0:3:32:a8:14:0
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.206 = STRING: 0:6:5b:38:72:bb
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.16 = STRING: 0:b0:d0:49:20:ca
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.20 = STRING: 0:b0:d0:49:44:63
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.246 = STRING: 0:b0:d0:49:44:82
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.216 = STRING: 0:b0:d0:49:44:e4
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.208 = STRING: 0:b0:d0:49:8B:89
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.209 = STRING: 0:d0:b7:8e:eb:96
.1.3.6.1.2.1.4.22.1.2.24.10.0.25.209 = STRING: 0:e0:81:2b:76:e7
-rich
0
 
Rich RumbleSecurity SamuraiAuthor Commented:
which fixed my issue for the most part... still there are 2 not showing up that should...
Mac address=00 00 0C 07 AC 08 port=1/1 IP =
Mac address=00 B0 D0 49 44 63 port=5/5 IP = 10.0.25.20
Mac address=00 03 32 A8 14 00 port=3/24 IP = 10.0.25.26
Mac address=00 B0 D0 49 8B 89 port=1/1 IP =
Mac address=00 B0 D0 49 20 CA port=5/6 IP = 10.0.25.16
Mac address=00 B0 D0 49 44 82 port=5/3 IP = 10.0.25.246
Mac address=00 E0 81 2B 76 E7 port=1/1 IP = 10.0.25.209
Mac address=00 B0 D0 49 44 E4 port=6/37 IP = 10.0.25.216
Mac address=00 06 5B 38 72 BB port=6/41 IP = 10.0.25.206
Mac address=00 D0 B7 8E EB 96 port=1/1 IP = 10.0.25.209

I'll check my files again, thanks again!
-rich
0
 
Rich RumbleSecurity SamuraiAuthor Commented:
I'll award the points now, its my fault (per usual) that the script didn't work ;) file5 still has some uppercase characters in it. Again I thank you for your time.
-rich
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now