Perl&Net :: SNMP :: Interfaces :: Details,如何获取mac地址?

| 对于我的实习,我必须给网络主管编码。我正在编写perl脚本以从交换机上的接口名称查找所有信息(速度,mac地址,双工...)。 该模块中有一个函数“ ifPhysAddress”,但是它返回交换机接口的mac地址,而不是与其连接的设备的mac地址。 我如何找到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;
    
已邀请:
您需要遵循某些网络拓扑算法。 为了找到主机和交换机/路由器之间的连接,必须首先从主机获取子网信息。然后找出从哪个交换机创建子网。如果找到具有该子网的交换机,则主机将连接到该交换机。 使用ifTable查找接口的ifIndex。 -> 53 使用ipRouteTable获取ipRouteDest。它将获得一些IP地址。它是表的主键。 -> 10.0.0.1、192.168.1.1、8.8.8.1 现在,使用ipRouteIfIndex + \“在步骤2中找到的IP”,找到每个跃点的ifIndex。\“将获取每个跃点的索引。 -> 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的索引。匹配第一步中的inIndex。对应的IP将是该接口上的IP。您可以通过直接查询该IP来获取MAC。 谢谢。     
好吧,如果有人需要,我找到了怎么做...
#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\";
我使用Net :: SNMP创建了一个SnmpTable.pm类,它确实可以: $ session-> get_table(-baseoid => $ this-> {oid}) 并以散列形式返回。 就这样。 再见     

要回复问题请先登录注册