Perl 和Net::SNMP::Interfaces::详细信息,如何获取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;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要遵循某些网络拓扑算法。
为了找到主机和交换机/路由器之间的连接,您必须首先从主机获取子网信息。然后找出子网是从哪个交换机创建的。如果在该子网中找到交换机,则主机将连接到该交换机。
--> 53
-->10.0.0.1、192.168.1.1、8.8.8.1
-->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.
--> 53
-->10.0.0.1, 192.168.1.1, 8.8.8.1
-->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.
好的,我找到了该怎么做,如果有人需要这样做...
我创建了一个类 SnmpTable.pm,使用 Net::SNMP,它就是这样做的:
$session->get_table( -baseoid => $this->{oid} )
并以散列形式返回它。
就这样。
再见。
Ok, i found how to do, if someone need to do it...
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.