Printing for debugging
am 14.01.2008 14:40:11 von Raful Mr Mitchell HThis is a multi-part message in MIME format.
size=3D2>#!/usr/bin/perl -w package = use = use strict;
size=3D2>use HTML::Template; use = use Socket qw(:DEFAULT = use dec_hex;
size=3D2>use snmpSession; use = use Net::NBName;
size=3D2>use Net::Ping; use Apache2::Request = use Apache2::Const =
size=3D2> # %seen_vlans --tracks those = # %device --all results for each device = # @answer;
size=3D2>my @switch_row; #--hold results of db query for = my @router_row; #--hold results of db = my = my %hash;
size=3D2>my $time; # --gets time query began
size=3D2>my $chosen_switch; #--switch passed via http form = my $chosen_router; #--switch passed via = my $switch_ip; #--result one = my $switch_model; = my = my $router_ip; # --result for = my $dbh; #--database = my $sql; #--database = my @loop_data; #--hold hashes for my = # $i =3D 0 = # $j =3D 0 = sub handler {
size=3D2>my $r =3D shift; my $req =3D =
size=3D2>$r->content_type('text/
class=3D894423513-14012008>html
size=3D2>my %mac_data; my @mac_array; #hold = my @loop_data =3D = my $template =3D =
size=3D2>die_on_bad_params =3D> 0 );
size=3D2> my $time =3D = $chosen_switch =3D =
size=3D2>$chosen_switch =3D~ s/(\s.*)//g;
size=3D2>$chosen_router =3D $req->param( 'chosen_router' = $dbh =3D DBI->connect( = { PrintError =3D> 1, =
size=3D2>$sql =3D "SELECT address, chassis_description FROM tblLiveCisco = @switch_row =
size=3D2>$switch_ip =3D $switch_row[0];
size=3D2>$switch_model =3D $switch_row[1];
size=3D2>@router_row =3D $dbh->selectrow_array("SELECT address FROM =
size=3D2>$router_ip =3D $router_row[0];
size=3D2>$devices_ref =3D getDevices( $router_ip, $switch_ip, = foreach $_ (@{$devices_ref}) = %hash =3D %$_;
size=3D2>push @loop_data, \%hash ;
size=3D2>} $template->param(CHOSEN_SWITCH =
size=3D2>$template->param(MODEL =3D> $switch_model = $template->param(TIME =3D> $time = $template->param(LOOP_DATA =3D> = print "Content-Type: = print = return = }
size=3D2>sub getDevices { my $router =3D = my $switch =3D = my $community =3D = my @mac_array =3D = my %seen_vlans =3D = my %ifnames; #holds the ifIndex number = my %trunk; #holds all = my = my @data;
size=3D2>my @vlans; #all vlans found using vmVlan = my $arp_mib =3D = my ( =
size=3D2> my $arp_session =3D =
size=3D2>my $vb_arp =3D new SNMP::Varbind( [$arp_mib] = for(my $var = ( = $var =3D $arp_session->getnext( = ) { my =
size=3D2>$ip_addr =3D~ s/$arp_mib\d+\.//g;
size=3D2>my @vars =3D split(/:/, $var );
size=3D2> for( my $x =3D 0; $x < scalar( = if( length( $vars[$x] ) = {
size=3D2>$vars[$x] =3D "0"."$vars[$x]";
size=3D2>} push( @mac_array, $vars[$x] = } my =
size=3D2>push( @{$arpCache{$arp_mac_address}}, $ip_addr = @mac_array =3D ();
size=3D2>} if( $arp_session->{ErrorNum} = my $vlan_session =3D =
size=3D2>my $vb_vlan =3D new SNMP::VarList( ['vmVlan'], ['ifName'], = my = if( $vlan_session->{ErrorNum} ) = print "Got ", = }
size=3D2>my $vlan_answer =3D $answers1[0];
size=3D2>my $ifName_answer =3D $answers1[1];
size=3D2>my $trunk_ports =3D $answers1[2];
size=3D2>for( my $i =3D 0; $i < scalar(@$vlan_answer); $i++ = { my $vlan =3D = if ( !exists = push( @vlans, = $seen_vlans{$vlan} =3D = }
size=3D2>} for( my $j =3D 0; $j < =
size=3D2>{ my $index =3D = my =
size=3D2>push @{$ifnames{$index}}, $interface_name;
size=3D2>} for = my $port_index =3D = if( =
size=3D2>{ $trunk{$port_index} =3D = }
size=3D2>} my $vb_cam =3D new SNMP::VarList( =
size=3D2>foreach $_ ( @vlans ) { $community = my $cam_session =3D =
size=3D2>my @answers2 =3D $cam_session->bulkwalk(0, 1000, $vb_cam = my $fdbPorts =3D = my $portIndexes =3D = for ( my $q =3D 0; $q < = for( my $r =
size=3D2>if( $fdbPorts->[$q]->[2] == =
size=3D2>{ $mac_address =3D = if( exists($arpCache{$mac_address} ) ) = $ip =3D = $dns_name = $device_type =3D = }
size=3D2>else { $ip =3D undef; } if( = {
size=3D2>if( $mac_address =3D~ /000255/o )
size=3D2>{ $device_type =3D = }
size=3D2>elsif( $mac_address =3D~ /000ff8/o )
size=3D2>{ $device_type =3D 'PSC = }
size=3D2>elsif( $mac_address =3D~ /00a0f8/g )
size=3D2>{ $device_type =3D 'Symbol RF = }
size=3D2>elsif( $mac_address =3D~ /001795/g )
size=3D2>{ $device_type =3D 'Cisco Systems = }
size=3D2>} my %device =3D (); = $device{PORT} =3D "@{$ifnames{ =
size=3D2>$device{MAC} =3D $mac_address;
size=3D2>$device{IP} =3D $ip;
size=3D2>$device{DEVICE} =3D $dns_name;
size=3D2>$device{TYPE} =3D $device_type;
size=3D2>push( @data, \%device );
size=3D2>}
size=3D2>} }
size=3D2>} return \@data;
size=3D2>} sub getName {
size=3D2>my $ip_address =3D shift(@_); my( =
size=3D2> $packed_binary_address =3D = $DNS =3D =
size=3D2>if( !$DNS ) { $nbQuery =3D = $nbStatus =3D = if( = {
size=3D2>$DNS =3D =
size=3D2>} if( !$DNS ) { $DNS =3D 'unknown' = } return = } sub = my $address =3D shift( @_ = my ( $type, $rtt, $tcp_print, $addr, =
size=3D2>$tcp_print =3D Net::Ping->new("syn");
size=3D2>$tcp_print->{port_num} =3D "9100";
size=3D2>$tcp_print->service_check( 1 );
size=3D2>$tcp_print->ping( $address );
size=3D2>if( ( $host, $rtt, $addr ) =3D $tcp_print->ack = { $type =3D = }
size=3D2>else{ $tcp_print =3D =
size=3D2>$tcp_print->{port_num} =3D "4500";
size=3D2>$tcp_print->service_check( 1 );
size=3D2>$tcp_print->ping( $address );
size=3D2>if( ( $host, $rtt, $addr ) =3D $tcp_print->ack = { $type =3D = }
size=3D2>} return $type;
size=3D2>} 1; Mitchell Raful MCSE CCNP
------_=_NextPart_001_01C856B2.FC34841D
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I have my code pasted below. My module, loaded with
directive in Apache2, works well. However, sometimes switches don't =
respond correctly to bulk queries of fdbPorts object. What I want to do =
is to be able print the value of my variables either to the browser or a =
log file. A regular print statement does not work for this. The =
relevant variables are in a subroutine called from my sub handler.
=20
Thanks in advance.
=20
#!/usr/bin/perl -w
package Handlers::PortMapper;
use warnings;
use strict;
use HTML::Template;
use DBI;
use Socket qw(:DEFAULT :crlf);
use dec_hex;
use snmpSession;
use SNMP;
use Net::NBName;
use Net::Ping;
use Apache2::Request ();
use Apache2::Const -compile=3D>'OK';
=20
# %seen_vlans --tracks those vlans which are seen as each ports vlan =
membership is looked at=20
# %device --all results for each device found on a switch are placed =
here for dispatching to a web page
# @answer;
my @switch_row; #--hold results of db query for switch
my @router_row; #--hold results of db query for router whose arp cache =
you want
my %devices_ref;
my %hash;
my $time; # --gets time query began
my $chosen_switch; #--switch passed via http form data
my $chosen_router; #--switch passed via http form data
my $switch_ip; #--result one for initial query
my $switch_model; #--result two for initial query
my $devices_ref;
my $router_ip; # --result for query of router address
my $dbh; #--database handle
my $sql; #--database query
my @loop_data; #--hold hashes for my template
# $i =3D 0 --counters
# $j =3D 0 --counters
sub handler {
my $r =3D shift;
my $req =3D Apache2::Request->new( $r );
$r->content_type('text/html');=20
my %mac_data;
my @mac_array; #hold individual hex digits returned by =
ipNetToMediaPhysAddress
my @loop_data =3D (); #array of anonymous hashes which is dispatched to =
web page for display
my $template =3D HTML::Template->new( filename =3D> =
'/home/mitch/www-dev/mod_perl/templates/portmapper.tmpl',
debug =3D> 1,
die_on_bad_params =3D> 0 );
my $time =3D localtime();
$chosen_switch =3D $req->param('chosen_switch');
$chosen_switch =3D~ s/(\s.*)//g;
$chosen_router =3D $req->param( 'chosen_router' );
$dbh =3D DBI->connect( 'dbi:ODBC:MRIServer2k', 'mriinventory', =
'Wysiwyg@3044',
{ PrintError =3D> 1, RaiseError =3D> 0, AutoCommit =3D> 1 } );
$sql =3D "SELECT address, chassis_description FROM tblLiveCisco WHERE =
name=3D'$chosen_switch'";
@switch_row =3D $dbh->selectrow_array( $sql );
$switch_ip =3D $switch_row[0];
$switch_model =3D $switch_row[1];
@router_row =3D $dbh->selectrow_array("SELECT address FROM tblLiveCisco =
WHERE name=3D'$chosen_router'" );
$router_ip =3D $router_row[0];
$devices_ref =3D getDevices( $router_ip, $switch_ip, 'public');
foreach $_ (@{$devices_ref}) {
%hash =3D %$_;
push @loop_data, \%hash ;
}
$template->param(CHOSEN_SWITCH =3D> $chosen_switch );
$template->param(MODEL =3D> $switch_model );
$template->param(TIME =3D> $time );
$template->param(LOOP_DATA =3D> \@loop_data );
print "Content-Type: text/html\n\n";
print $template->output;
return Apache2::Const::OK;
}
sub getDevices {
my $router =3D shift;
my $switch =3D shift;
my $community =3D shift;
my @mac_array =3D ();
my %seen_vlans =3D ();
my %ifnames; #holds the ifIndex number and names of ports
my %trunk; #holds all ports on a switch that are trunking
my %arpCache;
my @data;
my @vlans; #all vlans found using vmVlan object
my $arp_mib =3D 'ipNetToMediaPhysAddress';
my ( $mac_address,$ip, $dns_name, $device_type );
my $arp_session =3D snmpSession::openSession( $router , $community, 1 );
my $vb_arp =3D new SNMP::Varbind( [$arp_mib] );
for(my $var =3D $arp_session->getnext( $vb_arp );
( $vb_arp->tag eq $arp_mib ) and not ( $arp_session->{ErrorNum} );
$var =3D $arp_session->getnext( $vb_arp )
) {
my $ip_addr =3D ( $vb_arp->tag . $vb_arp->iid );
$ip_addr =3D~ s/$arp_mib\d+\.//g;
my @vars =3D split(/:/, $var );
for( my $x =3D 0; $x < scalar( @vars ); $x++ ) {
if( length( $vars[$x] ) < 2 )
{
$vars[$x] =3D "0"."$vars[$x]";
}
push( @mac_array, $vars[$x] );
}
my $arp_mac_address =3D join( "", @mac_array );
push( @{$arpCache{$arp_mac_address}}, $ip_addr );
@mac_array =3D ();
}
if( $arp_session->{ErrorNum} ){ print "Got $arp_session->{ErrorStr} for =
$router\n"; }
my $vlan_session =3D snmpSession::openSession ( $switch, $community, 1 =
);
my $vb_vlan =3D new SNMP::VarList( ['vmVlan'], ['ifName'], =
['vlanTrunkPortDynamicStatus'] );
my @answers1 =3D $vlan_session->bulkwalk(0, 100, $vb_vlan );
if( $vlan_session->{ErrorNum} ) {
print "Got ", $vlan_session->{ErrorStr}, "on", =
$vlan_session->{DestHost}, " during Bulkwalk\n";
}
my $vlan_answer =3D $answers1[0];
my $ifName_answer =3D $answers1[1];
my $trunk_ports =3D $answers1[2];
for( my $i =3D 0; $i < scalar(@$vlan_answer); $i++ )
{
my $vlan =3D $vlan_answer->[$i]->[ 2];
if ( !exists ($seen_vlans{$vlan} ) ) {
push( @vlans, $vlan );
$seen_vlans{$vlan} =3D 1;
}
}
for( my $j =3D 0; $j < scalar(@$ifName_answer); $j++ )
{
my $index =3D $ifName_answer->[$j]->[1];
my $interface_name =3D $ifName_answer->[$j]->[2];
push @{$ifnames{$index}}, $interface_name;
}
for ( my $p =3D 0; $p < scalar( @$trunk_ports ); $p++ ) {
my $port_index =3D $trunk_ports->[$p]->[1];
if( $trunk_ports->[$p]->[2] eq 'trunking' )
{
$trunk{$port_index} =3D 1;
}
}
my $vb_cam =3D new SNMP::VarList( ['dot1dTpFdbPort'], =
['dot1dBasePortIfIndex'] );
foreach $_ ( @vlans ) {
$community =3D "public@"."$_";
my $cam_session =3D snmpSession::openSession ( $switch, $community, 1 );
my @answers2 =3D $cam_session->bulkwalk(0, 1000, $vb_cam );
my $fdbPorts =3D $answers2[0];
my $portIndexes =3D $answers2[1];
for ( my $q =3D 0; $q < scalar( @$fdbPorts ); $q++ ) {
for( my $r =3D 0; $r < scalar( @$portIndexes ); $r++ ) {
if( $fdbPorts->[$q]->[2] == $portIndexes->[$r]->[1] && !exists( =
$trunk{ $portIndexes->[$r]->[2] } ) )
{
$mac_address =3D lc(dec_hex::dec_to_hex( $fdbPorts->[$q]->[1] ) );
if( exists($arpCache{$mac_address} ) ) {
$ip =3D $arpCache{$mac_address}->[0];
$dns_name =3D getName( $ip );
$device_type =3D getDeviceType( $ip );
}
else { $ip =3D undef; }
if( !defined( $ip ) )
{
if( $mac_address =3D~ /000255/o )
{
$device_type =3D 'Register';
}
elsif( $mac_address =3D~ /000ff8/o )
{
$device_type =3D 'PSC handheld';
}
elsif( $mac_address =3D~ /00a0f8/g )
{
$device_type =3D 'Symbol RF device';
}
elsif( $mac_address =3D~ /001795/g )
{
$device_type =3D 'Cisco Systems AP';
}
}
my %device =3D ();=20
$device{PORT} =3D "@{$ifnames{ $portIndexes->[$r]->[2]} }";
$device{MAC} =3D $mac_address;
$device{IP} =3D $ip;
$device{DEVICE} =3D $dns_name;
$device{TYPE} =3D $device_type;
push( @data, \%device );
}
}
}
}
return \@data;
}
sub getName {
my $ip_address =3D shift(@_);
my( $nbQuery, $nbStatus, $DNS, $packed_binary_address );
$packed_binary_address =3D inet_aton( $ip_address);
$DNS =3D gethostbyaddr( $packed_binary_address, AF_INET );
if( !$DNS ) {
$nbQuery =3D Net::NBName->new;
$nbStatus =3D $nbQuery->node_status( $ip_address );
if( defined $nbStatus )
{
$DNS =3D $nbStatus->{names}->[0]->{name};
}
if( !$DNS ) { $DNS =3D 'unknown' }
}
return $DNS;
}
sub getDeviceType {
my $address =3D shift( @_ );
my ( $type, $rtt, $tcp_print, $addr, $host);
$tcp_print =3D Net::Ping->new("syn");
$tcp_print->{port_num} =3D "9100";
$tcp_print->service_check( 1 );
$tcp_print->ping( $address );
if( ( $host, $rtt, $addr ) =3D $tcp_print->ack )
{
$type =3D "Printer";
}
else{
$tcp_print =3D Net::Ping->new("syn");
$tcp_print->{port_num} =3D "4500";
$tcp_print->service_check( 1 );
$tcp_print->ping( $address );
if( ( $host, $rtt, $addr ) =3D $tcp_print->ack )
{
$type =3D "Controller"
}
}
return $type;
}
1;
Mitchell Raful MCSE CCNP=20
MCB Quantico=20
Personal and Family Readiness (MRI)=20
3044 Catlin Avenue=20
Quantico, VA 22134=20
Com: 703-784-5991=20
DSN: 278-5991=20
Cell: 804-363-0731=20
=20
------_=_NextPart_001_01C856B2.FC34841D
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
charset=3Diso-8859-1">
my code=20
pasted below. My module, loaded with <Location> directive in =
Apache2, works well. However, sometimes switches don't respond =
correctly=20
to bulk queries of fdbPorts object. What I want to do is to be =
able print=20
the value of my variables either to the browser or a log file. A =
regular=20
print statement does not work for this. The relevant variables are =
in a=20
subroutine called from my sub handler.
in=20
advance.
');
Handlers::PortMapper;
warnings;
DBI;
:crlf);
SNMP;
();
-compile=3D>'OK';
vlans which are seen as each ports vlan membership is looked at =
found on a switch are placed here for dispatching to a web =
page
switch
query for router whose arp cache you want
%devices_ref;
data
http form data
for initial query
#--result two for initial query
$devices_ref;
query of router address
handle
query
template
--counters
--counters
Apache2::Request->new( $r );
individual hex digits returned by =
ipNetToMediaPhysAddress
(); #array of anonymous hashes which is dispatched to web page for =
display
HTML::Template->new( filename =3D> =
'/home/mitch/www-dev/mod_perl/templates/portmapper.tmpl',
localtime();
$req->param('chosen_switch');
);
'dbi:ODBC:MRIServer2k', 'mriinventory', =
'Wysiwyg@3044',
RaiseError =3D> 0, AutoCommit =3D> 1 } );
WHERE name=3D'$chosen_switch'";
=3D $dbh->selectrow_array( $sql );
tblLiveCisco WHERE name=3D'$chosen_router'" );
'public');
{
=3D> $chosen_switch );
);
);
\@loop_data );
text/html\n\n";
$template->output;
Apache2::Const::OK;
shift;
shift;
shift;
();
();
and names of ports
ports on a switch that are trunking
%arpCache;
object
'ipNetToMediaPhysAddress';
$mac_address,$ip, $dns_name, $device_type );
snmpSession::openSession( $router , $community, 1 );
);
=3D $arp_session->getnext( $vb_arp );
$vb_arp->tag eq $arp_mib ) and not ( $arp_session->{ErrorNum} =
);
$vb_arp )
$ip_addr =3D ( $vb_arp->tag . $vb_arp->iid );
@vars ); $x++ ) {
< 2 )
);
$arp_mac_address =3D join( "", @mac_array );
);
){ print "Got $arp_session->{ErrorStr} for $router\n"; =
}
snmpSession::openSession ( $switch, $community, 1 );
['vlanTrunkPortDynamicStatus'] );
@answers1 =3D $vlan_session->bulkwalk(0, 100, $vb_vlan =
);
{
$vlan_session->{ErrorStr}, "on", $vlan_session->{DestHost}, " =
during Bulkwalk\n";
)
$vlan_answer->[$i]->[ 2];
($seen_vlans{$vlan} ) ) {
$vlan );
1;
scalar(@$ifName_answer); $j++ )
$ifName_answer->[$j]->[1];
$interface_name =3D $ifName_answer->[$j]->[2];
( my $p =3D 0; $p < scalar( @$trunk_ports ); $p++ ) =
{
$trunk_ports->[$p]->[1];
$trunk_ports->[$p]->[2] eq 'trunking' )
1;
['dot1dTpFdbPort'], ['dot1dBasePortIfIndex'] );
=3D "public@"."$_";
snmpSession::openSession ( $switch, $community, 1 );
);
$answers2[0];
$answers2[1];
scalar( @$fdbPorts ); $q++ ) {
=3D 0; $r < scalar( @$portIndexes ); $r++ ) {
$portIndexes->[$r]->[1] && !exists( $trunk{ =
$portIndexes->[$r]->[2] } ) )
lc(dec_hex::dec_to_hex( $fdbPorts->[$q]->[1] ) =
);
{
$arpCache{$mac_address}->[0];
=3D getName( $ip );
getDeviceType( $ip );
!defined( $ip ) )
'Register';
handheld';
device';
AP';
$portIndexes->[$r]->[2]} }";
$nbQuery, $nbStatus, $DNS, $packed_binary_address );
inet_aton( $ip_address);
gethostbyaddr( $packed_binary_address, AF_INET );
Net::NBName->new;
$nbQuery->node_status( $ip_address );
defined $nbStatus )
$nbStatus->{names}->[0]->{name};
}
$DNS;
getDeviceType {
);
$host);
)
"Printer";
Net::Ping->new("syn");
)
"Controller"
face=3DArial=20
size=3D2>MCB Quantico
Personal =
and Family=20
Readiness (MRI)
3044 Catlin =
Avenue=20
Quantico, VA 22134
face=3DArial=20
size=3D2>Com: 703-784-5991
DSN: =
278-5991=20
Cell: 804-363-0731
------_=_NextPart_001_01C856B2.FC34841D--