Reputation: 5658
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;
Upvotes: 3
Views: 6390
Reputation: 5658
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.
Upvotes: 1
Reputation: 3128
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.
Thanks.
Upvotes: 1