#!/usr/bin/env perl use strict; use warnings; =head1 NAME ifinfo.pl =head1 ABSTRACT A script to get the ifTable and ifXTable info from switches supporting the MIBs. =head1 SYNOPSIS ifinfo.pl OPTIONS agent agent ... ifinfo.pl OPTIONS -i if $from_stdin; chomp @agents; usage('missing agents') unless @agents; my @sessions; foreach my $agent ( sort @agents ) { my ( $session, $error ) = Net::SNMP->session( -community => $community, -hostname => $agent, -version => $version, -nonblocking => $nonblocking, -timeout => $timeout, -retries => $retries, -debug => $debug ? DEBUG_ALL: 0, ); if ($error) { warn $error; next; } $session->mixer(qw/Net::SNMP::Mixin::IfInfo/); $session->init_mixins; push @sessions, $session; } snmp_dispatcher() if $Net::SNMP::NONBLOCKING; # remove sessions with error from the sessions list @sessions = grep { warn $_->error if $_->error; not $_->error } @sessions; print_if(); exit 0; ###################### end of main ###################### sub print_if { foreach my $session ( sort { $a->hostname cmp $b->hostname } @sessions ) { print "\n"; printf "Hostname: %-15.15s\n", $session->hostname; print '-' x 78, "\n"; printf "%5s %5s %6s %10s %26s %21s\n", 'ifIdx', 'ad/op', 'ifName', 'ifDesc', 'ifAlias', 'ifType'; print '-' x 78, "\n"; my $if_entries = $session->get_if_entries; foreach my $if_index ( sort { $a <=> $b } keys %$if_entries ) { my $ifAdminStatus = $if_entries->{$if_index}->{ifAdminStatus} || 0; my $ifOperStatus = $if_entries->{$if_index}->{ifOperStatus} || 0; my $ifName = $if_entries->{$if_index}->{ifName} || ''; my $ifDescr = $if_entries->{$if_index}->{ifDescr} || ''; my $ifType = $if_entries->{$if_index}->{ifType} || ''; my $ifAlias = $if_entries->{$if_index}->{ifAlias} || ''; printf "%5d %1d/%1d %-10.10s %-25.25s %-22.22s %2d\n", $if_index, $ifAdminStatus, $ifOperStatus, $ifName, $ifDescr, $ifAlias, $ifType; } print "\n"; } } sub usage { my @msg = @_; die <>>>>> @msg Usage: $0 [options] hostname -c community -v version -t timeout -r retries -d Net::SNMP debug on -i read agents from stdin -B nonblocking EOT } =head1 AUTHOR Karl Gaissmaier, karl.gaissmaier (at) uni-ulm.de =head1 COPYRIGHT Copyright (C) 2008 by Karl Gaissmaier This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut # vim: sw=2