2011-04-13 47 views
3

为了我的实习,我必须编码网络主管。我正在写perl脚本以从交换机上的接口名称查找所有信息(速度,mac地址,双工...)。 该模块中有一个“ifPhysAddress”函数,但它返回交换机接口的mac地址,而不是与其连接的设备的mac地址。 我如何找到mac地址? 感谢Perl&Net :: SNMP :: Interfaces ::详细信息,如何获取mac地址?

这里是我已经开始:

#!/usr/bin/perl 
use strict; 
use warnings; 
use Getopt::Long; 
use SnmpTable; 
use Net::MAC; 
use Net::SNMP; 
use Net::SNMP::Interfaces; 

my $ifname; 
my $hostname; 
my $community; 
my $version = 1; 

GetOptions("ifname=s"  => \$ifname, 
      "host=s"  => \$hostname, 
      "community=s" => \$community, 
      "protocol:s" => \$version); 

my $interfaces = Net::SNMP::Interfaces->new(Hostname => $hostname, Community => $community); 
my $inter = $interfaces->interface($ifname); 

#On récupere l'identifiant de l'interface $ifname 
my $ifindex = $inter->index(); 
#Vitesse 
my $vitesse = $inter->ifHighSpeed(); 
#Alias 
my $ifalias = $inter->ifAlias(); 


#Recherche des VLANs 
my $numeroportbridge; 
my $vlan_trouve; 

my $oid_cisco_vlans = "1.3.6.1.4.1.9.9.46.1.3.1.1.2.1"; 
my $vlans = SnmpTable->new($hostname, $oid_cisco_vlans, $community); 
$vlans->connexion(); 
my %vl = $vlans->requete(); 
my @tab = keys(%vl); 

foreach my $i (@tab) { 
    if ($i<1000) { 
     my $comvlan = $community."@".$i; 
     print $comvlan."\n"; 
    } 
} 
printf "Nom de l'interface : %s --> ifindex = %s, Vitesse = %s, Alias = %s\n", $ifname, $ifindex, $vitesse, $ifalias; 

回答

1

您需要按照一定的网络拓扑算法。

为了找到主机和交换机/路由器之间的连接,您必须先从主机获取子网信息。然后找出从哪个交换机创建子网。如果在该子网中找到交换机,则主机连接到该交换机。

  1. 查找的ifIndex为您操作界面采用ifTable的
    - > 53
  2. 使用ipRouteTable获取ipRouteDest。它会得到一些IP地址。这是表格的主要关键。
    - > 10.0.0.1,192.168.1.1,8.8.8.1
  3. 现在,找到的ifIndex使用ipRouteIfIndex +每一跳 “IP在步骤2中发现”它会得到每个跳跃的索引。
    - > 1.3.6.1.2.1.4.21.1.2(ipRouteIfIndex)+10.0.0.1 = 1.3.6.1.2.1.4.21.1.2.10.0.0.1
    - >在步骤3中的查询的响应,你会得到如果该IP的索引。匹配来自第一步的索引。对应的IP将是该接口的IP。您可以通过直接查询该IP来获得MAC。

谢谢。

+0

好吧,我明白了,但我不知道主机。我想调用我的脚本: - switch ip - community - 接口名称(例如FastEthernet0/9) 我想让他回答:在这个接口上Fa0/9连接到“主机的IP” ,速度:100,双面打印:全... – eouti 2011-04-13 12:09:42

+0

好的。然后找到从接口Fa0/9创建的子网。从该子网生成全部256个IP地址。 Ping所有IP并找到活的IP。然后在我的答案中匹配条件。 – nIKUNJ 2011-04-13 12:15:12

+0

对不起,我犯了一个错误,我不在乎主机的IP,我更喜欢主机的MAC。它更容易吗?谢谢。 – eouti 2011-04-13 12:36:17

1

好吧,我发现怎么办,如果有人需要做...

#We get the index of $ifname 
my $ifindex = $inter->index(); 
#Speed 
my $vitesse = $inter->ifHighSpeed(); 
#Alias 
my $ifalias = $inter->ifAlias(); 
#Finding VLANs 
my $vlan_trouve; 

#Listing all VLANs on the switch 
my $vmVlan = "1.3.6.1.4.1.9.9.68.1.2.2.1.2"; 
my $vlans = SnmpTable->new($hostname, $vmVlan, $community); 
$vlans->connexion(); 
my %vl = $vlans->requete(); 

#Getting the good VLAN 
$vlan_trouve = $vl{$ifindex}; 

#Listing ports of VLAN <-> @mac 
my $dot1dTpFdbAddress = "1.3.6.1.2.1.17.4.3.1.1"; 
my $dot = SnmpTable->new($hostname, $dot1dTpFdbAddress, $community."@".$vlan_trouve); 
$dot->connexion(); 
my %dot1address = $dot->requete(); 

#Listing numPortBridge <-> ports of VLAN 
my $dot1dTpFdbPort = "1.3.6.1.2.1.17.4.3.1.2"; 
my $dot2 = SnmpTable->new($hostname, $dot1dTpFdbPort, $community."@".$vlan_trouve); 
$dot2->connexion(); 
my %portdot = reverse($dot2->requete()); 

#Listing num Port bridge <-> ID port switch 
my $dot1dBasePortIfIndex = "1.3.6.1.2.1.17.1.4.1.2"; 
my $dot3 = SnmpTable->new($hostname, $dot1dBasePortIfIndex, $community."@".$vlan_trouve); 
$dot3->connexion(); 
my %dotindex = reverse($dot3->requete()); 

my $numportbridge = $dotindex{$ifindex}; 
if (!defined($numportbridge)) { 
    print "Erreur : $ifindex non trouvé dans la liste : num Port bridge <-> ID port switch\n"; 
    exit 2; 
} 
my $portVlan = $portdot{$numportbridge}; 
if (!defined($portVlan)) { 
    print "Erreur : $numportbridge non trouvé dans la liste : numPortBridge <-> ports du VLAN\n"; 
    exit 3; 
} 
my $add = $dot1address{$portVlan}; 
if (!defined($add)) { 
    print "Erreur : $portVlan non trouvé dans la liste : ports du VLAN <-> \@mac\n"; 
    exit 4; 
} 
$add =~ s/0x//g; 
printf "Interface : $ifname sur $hostname <=> ifindex : $ifindex sur VLAN : $vlan_trouve <=> \@mac : $add\nVitesse=$vitesse, Alias=$ifalias, Duplex=--\n"; 

我创建了一个类SnmpTable.pm,使用的Net :: SNMP,它只是做:
$会议 - > get_table(-baseoid => $ this - > {oid})
并以散列形式返回它。

就是这样。 再见。

+0

你碰巧拥有你的SnmpTable.pm源代码吗?一直在努力创造一个,但我做错了什么。 – elysch 2017-07-10 14:51:29