Difference between revisions of "Net: : SNMP vis interface med request get"
From Teknologisk videncenter
m |
|||
Line 38: | Line 38: | ||
# | # | ||
### Pragma's | ### Pragma's | ||
+ | |||
use strict; | use strict; | ||
use warnings; | use warnings; | ||
− | use Socket; | + | #use Socket; |
use Net::SNMP qw(:snmp); | use Net::SNMP qw(:snmp); | ||
+ | use snmpsubs; | ||
my $IfNumbers = '1.3.6.1.2.1.2.1.0'; | my $IfNumbers = '1.3.6.1.2.1.2.1.0'; | ||
Line 67: | Line 69: | ||
#################### SUBS ###################### | #################### SUBS ###################### | ||
+ | # Alle subrutiner ligger i snmpsubs.pm | ||
+ | |||
+ | ##################### MAIN ###################### | ||
+ | |||
+ | foreach (@INC){ | ||
+ | print ">$_\n"; | ||
+ | } | ||
+ | print "Indtast ip eller host: "; | ||
+ | my $host = <>; | ||
+ | chomp $host; | ||
+ | |||
+ | $ip = snmpsubs->RealIp($host); | ||
+ | |||
+ | ## Sæt session op | ||
+ | my ($session, $error) = Net::SNMP->session( | ||
+ | -hostname => $ip, | ||
+ | -version => '2c', ); | ||
+ | |||
+ | if (!defined($session)) { | ||
+ | printf("Kan ikke åbne session til $host fejl: $error\n"); | ||
+ | } | ||
+ | |||
+ | ## Hent interface antal | ||
+ | print "Henter antal interfaces: \n"; | ||
+ | printf " - Der er %s interfaces.\n", ($intnr = snmpsubs->GetIntNrs ($session)); | ||
+ | |||
+ | print "Følgende findes:\n"; | ||
+ | for ($i = 1; $i <= $intnr; $i++) { | ||
+ | printf "%s - %s\n", $i, (snmpsubs->GetIntInfo ($session, "$IfListStart"."$IfProp{'Description'}".".$i")); | ||
+ | } | ||
+ | |||
+ | ## Hvilket interface skal vises | ||
+ | print "Hvilket interface skal vises. Vælg mellem 1 og $intnr: "; | ||
+ | $interface = <>; | ||
+ | chomp $interface; | ||
+ | |||
+ | if ( $interface >= $intnr ) { | ||
+ | print "Dette interface findes ikke...\n"; | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | ## Præsenter interfacet | ||
+ | foreach $key ( keys %IfProp ){ | ||
+ | printf "%12s --> %s\n", $key, (snmpsubs->GetIntInfo ($session, "$IfListStart"."$IfProp{$key}".".$interface")); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ===Snmpsubs.pm=== | ||
+ | <source lang=perl> | ||
+ | package snmpsubs; | ||
+ | |||
+ | # Filename: snmpsubs.pm | ||
+ | |||
+ | use base 'Exporter'; | ||
+ | our @EXPORT_OK = qw(RealIp GetIntNrs GetIntInfo); | ||
+ | use strict; | ||
+ | use warnings; | ||
+ | |||
+ | use Socket; | ||
+ | use Net::SNMP qw(:snmp); | ||
+ | |||
+ | my $IfNumbers = '1.3.6.1.2.1.2.1.0'; | ||
+ | my $IfListStart ='1.3.6.1.2.1.2.2.1.'; | ||
+ | my %IfProp = ("Description"=>2, | ||
+ | "Type"=>3, | ||
+ | "MTU"=>4, | ||
+ | "Speed"=>5, | ||
+ | "MAC"=>6, | ||
+ | "AdminStatus"=>7, | ||
+ | "InOctets"=>10, | ||
+ | "OutOctets"=>16, | ||
+ | "InErrors"=>14, | ||
+ | "OutErrors"=>20 | ||
+ | ); | ||
+ | |||
+ | |||
sub RealIp { | sub RealIp { | ||
## Find real ip address | ## Find real ip address | ||
+ | shift; | ||
my $pip = shift; | my $pip = shift; | ||
+ | my $addr; | ||
+ | print "RI $pip\n"; | ||
if ( $pip =~ /^[1-9][0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$/ ){ | if ( $pip =~ /^[1-9][0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$/ ){ | ||
Line 96: | Line 177: | ||
# OutPut : antal If | # OutPut : antal If | ||
+ | shift; | ||
my $session = shift; | my $session = shift; | ||
my $OID; | my $OID; | ||
Line 104: | Line 186: | ||
if ( ! defined $result) { | if ( ! defined $result) { | ||
− | + | die "Arrrrr - Kiks i GetIntNrs!!!\n"; | |
} | } | ||
Line 118: | Line 200: | ||
# Output : Tekstinfo | # Output : Tekstinfo | ||
+ | shift; | ||
my $session = shift; | my $session = shift; | ||
my $OID = shift; | my $OID = shift; | ||
Line 125: | Line 208: | ||
if ( ! defined $result) { | if ( ! defined $result) { | ||
− | + | die "Arrrrr - Kiks i GetIntInfo!!!\n"; | |
} | } | ||
Line 132: | Line 215: | ||
} | } | ||
− | + | 1; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
--[[User:Tcj|Tomas Christian Jensen]] 09:45, 12 November 2009 (CET) | --[[User:Tcj|Tomas Christian Jensen]] 09:45, 12 November 2009 (CET) | ||
[[Category:Perl]] | [[Category:Perl]] |
Revision as of 11:54, 12 November 2009
Hvad gør scriptet
Scriptet finder alle interfaces i udstyret. Derefter beder det efter det interface der skal undersøges.
Herefter vises diverse tællere og anden information for interfacet.
Output fra script
Indtast ip eller host: localhost Henter antal interfaces: - Der er 5 interfaces. Følgende findes: 1 - lo 2 - eth0 3 - wmaster0 4 - wlan0 5 - pan0 Hvilket interface skal vises. Vælg mellem 1 og 5: 4 InOctets --> 53325141 MAC --> 0x001cbf53933c InErrors --> 0 OutOctets --> 5236357 MTU --> 1500 Type --> 6 Speed --> 10000000 AdminStatus --> 1 OutErrors --> 0 Description --> wlan0
Koden
#!/usr/bin/perl
#######################################
# Author: Tomas Jensen / TDC #
# Email.: tcj@tdc.dk #
#######################################
#
### Pragma's
use strict;
use warnings;
#use Socket;
use Net::SNMP qw(:snmp);
use snmpsubs;
my $IfNumbers = '1.3.6.1.2.1.2.1.0';
my $IfListStart ='1.3.6.1.2.1.2.2.1.';
my %IfProp = ("Description"=>2,
"Type"=>3,
"MTU"=>4,
"Speed"=>5,
"MAC"=>6,
"AdminStatus"=>7,
"InOctets"=>10,
"OutOctets"=>16,
"InErrors"=>14,
"OutErrors"=>20
);
my $intnr;
my %interfaces;
my $interface;
my $key;
my $value;
my $ip;
my $addr;
my @result;
my $i;
#################### SUBS ######################
# Alle subrutiner ligger i snmpsubs.pm
##################### MAIN ######################
foreach (@INC){
print ">$_\n";
}
print "Indtast ip eller host: ";
my $host = <>;
chomp $host;
$ip = snmpsubs->RealIp($host);
## Sæt session op
my ($session, $error) = Net::SNMP->session(
-hostname => $ip,
-version => '2c', );
if (!defined($session)) {
printf("Kan ikke åbne session til $host fejl: $error\n");
}
## Hent interface antal
print "Henter antal interfaces: \n";
printf " - Der er %s interfaces.\n", ($intnr = snmpsubs->GetIntNrs ($session));
print "Følgende findes:\n";
for ($i = 1; $i <= $intnr; $i++) {
printf "%s - %s\n", $i, (snmpsubs->GetIntInfo ($session, "$IfListStart"."$IfProp{'Description'}".".$i"));
}
## Hvilket interface skal vises
print "Hvilket interface skal vises. Vælg mellem 1 og $intnr: ";
$interface = <>;
chomp $interface;
if ( $interface >= $intnr ) {
print "Dette interface findes ikke...\n";
exit;
}
## Præsenter interfacet
foreach $key ( keys %IfProp ){
printf "%12s --> %s\n", $key, (snmpsubs->GetIntInfo ($session, "$IfListStart"."$IfProp{$key}".".$interface"));
}
Snmpsubs.pm
package snmpsubs;
# Filename: snmpsubs.pm
use base 'Exporter';
our @EXPORT_OK = qw(RealIp GetIntNrs GetIntInfo);
use strict;
use warnings;
use Socket;
use Net::SNMP qw(:snmp);
my $IfNumbers = '1.3.6.1.2.1.2.1.0';
my $IfListStart ='1.3.6.1.2.1.2.2.1.';
my %IfProp = ("Description"=>2,
"Type"=>3,
"MTU"=>4,
"Speed"=>5,
"MAC"=>6,
"AdminStatus"=>7,
"InOctets"=>10,
"OutOctets"=>16,
"InErrors"=>14,
"OutErrors"=>20
);
sub RealIp {
## Find real ip address
shift;
my $pip = shift;
my $addr;
print "RI $pip\n";
if ( $pip =~ /^[1-9][0-9]*\.[0-9]+\.[0-9]+\.[0-9]+$/ ){
# Vi har en aegte ip
return $pip;
} else {
if ( $pip =~ /^0.*/ ) {
# Fjern indledende 0'er
$pip =~ s/^0+//;
return $pip;
} else {
# lav get host by name
$addr = inet_aton("$pip");
$addr = inet_ntoa ($addr);
if ( defined $addr ){
return $addr;
}
}
}
}
sub GetIntNrs {
## Find og returner antal interfaces
# Input : session
# OutPut : antal If
shift;
my $session = shift;
my $OID;
my $ifn;
my $result = $session->get_request(
-varbindlist => [$IfNumbers]);
if ( ! defined $result) {
die "Arrrrr - Kiks i GetIntNrs!!!\n";
}
($OID, $ifn) = each %{$result};
return $ifn;
}
sub GetIntInfo {
## Hent et interface
# Input : session
# Input : OID
# Output : Tekstinfo
shift;
my $session = shift;
my $OID = shift;
my $info;
my $result = $session->get_request(
-varbindlist => [$OID]);
if ( ! defined $result) {
die "Arrrrr - Kiks i GetIntInfo!!!\n";
}
($OID, $info) = each %{$result};
return $info;
}
1;
--Tomas Christian Jensen 09:45, 12 November 2009 (CET)