博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

系统※网络技术实验室

System & Databses & Networking & Security & Services
evegl.cublog.cn
nmap和交换机MAC表关联IP
#!/usr/bin/perl
my $ipfile="./1.0-mac";
my $macfile="./1.234";
open MAC,"$macfile" or die "Can't open file $macfile:$!\n";
@mac=<MAC>;
close(MAC);
open IP,"$ipfile" or die "Can't open file $ipfile:$!\n";
@ip=<IP>;
close(IP);
%switch=();
foreach (@mac) {
 chomp;
 m/\w+\.\w+\.(\w+)\s+\w+\s+(.*)/ig;
 $switch{$1}=$2;
}
$flag=0;
$count=$#ip;
for ( $i=0;$i<$count;$i++ ) {
 chomp($nmap=$ip[$i]); 
 if ($nmap=~/(\d+\.\d+\.\d+\.\d+)/g) {
  $flag++;
  $num=$flag-$i;
  if ( $num == 1 ) {
   push(@hosts,$1);
   $flag++;
  }
 }
 if ($nmap=~/\w+:\w+:\w+:\w+:(\w+:\w+)/ig){
  ($mac1,$mac2) = split(/:/,$1);
  $submac = lc($mac1.$mac2);
  push(@macs,$submac);
 }
}
%host=();
$count=$#hosts+1;
for ( $i=0 ;$i < $count;$i++ ) {
 $host{$macs[$i]}=$hosts[$i];
}
foreach $smac ( keys %switch ) {
 if (defined $host{$smac}){
  print "MAC: $smac \tHost: $host{$smac} \tInterface: $switch{$smac}\n";
 }
}
 
 
上面的有些逻辑问题,不知道怎么调整了,同事用的是新的思路,不错.
 
#!/usr/bin/perl
die "Usage: <data file>\n"
unless @ARGV == 1;
my $ipfile="./1.0-mac";
my $macfile="$ARGV[0]";
my %hash2;
open(FILE,"<$macfile") or die("can't open file:$!");
while (<FILE>) {
  chomp;
  $_=~s/(\w+)\.(\w+)\.(\w+)/$1:$2:$3/gi;
  $_=~/(\w+:\w+:\w+).*?(Fa\d\/\d+)/;
  $mac2=uc($1);
  $hash2{($mac2)}=$2;
}
close(FILE);
open(FILE1,"<$ipfile") or die("can't open file:$!");
$/=undef;
$array=<FILE1>;
$array=~s/up\.\nMAC Address:/up.MAC Address:/sig;
$/="\n";
my @array1=split/\n/,$array;
my %hash;
foreach(@array1) {
  chomp;
  $_=~/\w+\s(\d+\.\d+\.\d+\.\d+).*?(\w+):(\w+):(\w+):(\w+):(\w+):(\w+)/;
  $mac="$2$3:$4$5:$6$7";
  $hash{$mac}=$1;
}
foreach $macs(keys(%hash2)) {
  foreach (keys(%hash)) {
    if($macs eq $_) {
      print "MAC: $macs \tHost: $hash{$_} \tInterface: $hash2{$macs}\n";
    }
  }
}
 
最新修改:
#!/usr/bin/perl
die "Usage: <data file> <siwtch-mac>\n"
unless @ARGV == 2;
my $ipfile=$ARGV[0];
my $macfile=$ARGV[1];
my %hash2;
open(FILE,"<$macfile") or die("can't open file:$!");
while (<FILE>) {
  chomp;
  $_=~s/(\w+)\.(\w+)\.(\w+)/$1:$2:$3/gi;
  $_=~/(\w+:\w+:\w+).*?((Fa|GigabitEthernet|Gi)\d\/\d(\/|)?(.*|)?)/;
  $mac2=uc($1);
  $hash2{($mac2)}=$2;
}
close(FILE);
open(FILE1,"<$ipfile") or die("can't open file:$!");
$/=undef;
$array=<FILE1>;
$array=~s/up\.\nMAC Address:/up.MAC Address:/sig;
$/="\n";
my @array1=split/\n/,$array;
my %hash;
foreach(@array1) {
  chomp;
  $_=~/.*?\w+\s.*?(\d+\.\d+\.\d+\.\d+).*?(\w+):(\w+):(\w+):(\w+):(\w+):(\w+)/;
  $mac="$2$3:$4$5:$6$7";
  $hash{$mac}=$1;
}
foreach $macs(keys(%hash2)) {
  foreach (keys(%hash)) {
    if($macs eq $_) {
      print "MAC: $macs \tHost: - Link_To_$hash{$_} \tInterface: $hash2{$macs}\n";
    }
  }
}

发表于: 2007-11-29,修改于: 2008-04-10 10:11,已浏览330次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:0.05959