gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:32:10 24 4
gpt4 key购买 nike

在我的实习期间,我必须编写一名网络主管。我正在编写 perl 脚本以从交换机上的接口(interface)名称中查找所有信息(速度、mac 地址、双工...)。该模块中有一个函数“ifPhysAddress”,但它返回的是交换机接口(interface)的 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;

最佳答案

您需要遵循特定的网络拓扑算法。

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

  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 将是该接口(interface)的 IP。直接查询那个IP就可以得到MAC。

谢谢。

关于Perl & Net::SNMP::Interfaces::Details,如何获取mac地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5647294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com