Perl 和Net::SNMP::Interfaces::详细信息,如何获取mac地址?

发布于 2024-10-31 19:58:38 字数 1410 浏览 6 评论 0原文

在我的实习中,我必须编写一个网络主管的代码。我正在编写 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;

for my internship i have to code a network supervisor. I'm writting perl scripts to find all informations (speed, mac address, duplex...) from an interface name on the switch.
There is a function "ifPhysAddress" in this module, but it returns the switch interface mac address, not the mac address of the device connected to it.
How can I find the mac address please ?
Thanks

Here what I've started :

#!/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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-11-07 19:58:38

您需要遵循某些网络拓扑算法。

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

  1. 使用ifTable为您的界面查找ifIndex
    --> 53
  2. 使用ipRouteTable获取ipRouteDest。它将获得一些IP地址。它是表的主键。
    -->10.0.0.1、192.168.1.1、8.8.8.1
  3. 现在,使用 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。

谢谢。

You need to follow certain Network Topology algorithms.

For finding connection between a Host machine and a Switch/Router, you have to fist get subnet information from the host machine. Then find out from which switch, the subnet is created. If switch is found with that subnet then the host is connected to that switch.

  1. Find ifIndex for you interface using ifTable.
    --> 53
  2. Get ipRouteDest using ipRouteTable. It will get some ip addresses. It is primary key for the table.
    -->10.0.0.1, 192.168.1.1, 8.8.8.1
  3. Now, find ifIndex for each hop using ipRouteIfIndex+"ip found in step 2." It will get if indexes for each hop.
    -->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
    --> In response of queries in step 3, you will get if index for that IP. Match inIndex from step one. The correnspoding IP will be the IP at that interface. You can get MAC by directly querying that IP.

Thanks.

终难遇 2024-11-07 19:58:38

好的,我找到了该怎么做,如果有人需要这样做...

#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,它就是这样做的:
$session->get_table( -baseoid => $this->{oid} )
并以散列形式返回它。

就这样。
再见。

Ok, i found how to do, if someone need to do it...

#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";

I created a class SnmpTable.pm, using Net::SNMP, it just does :
$session->get_table( -baseoid => $this->{oid} )
and returns it in a hash.

That's all.
Bye.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文