[c-nsp] snmpwalk for switch port status

Brian Raaen braaen at zcorum.com
Wed Nov 18 09:02:49 EST 2009


try this, written for Debian Linux so may or may not need modification to run 
on your system.




#!/bin/bash

comunity=
host=
group=
list=
output=

if [ "$#" == "0" ]
then
        echo "$0: No Arguments.... please put at least a host" >&2
        echo "Usage: $0 [-c community_string] [-l list_of_snmp_indexes_file] 
[-o file_to_output_to] hostname" >&2
        exit 1
fi

while getopts :c:l:o:h opt
do
        case $opt in
        c)      community="$OPTARG"
                ;;
        l)      list="$OPTARG"
                ;;
        o)      output="$OPTARG"
                ;;
        h)      echo "Usage: $0 [-c community_string] [-g 
nagios_contact_group] [-l list_of_snmp_indexes_file] [-o file_to_output_to] 
hostname" >&2
                exit 0
                ;;
        '?')    echo "$0: invalid option -$OPTARG" >&2
                echo "Usage: $0 [-c community_string] [-g 
nagios_contact_group] [-l list_of_snmp_indexes_file] [-o file_to_output_to] 
hostname" >&2
                exit 1
                ;;
        esac
done

shift $((OPTIND - 1))


host="$1"

if [ ! $community ]
then
        community="public"
fi

if [ $list ]
then
        list=`cat $list`
else
        list=`snmpwalk -v 2c -c $community -Oe $host 1.3.6.1.2.1.31.1.1.1.1 | 
egrep -v "( STRING: Nu0| STRING: T1 | STRING: Lo| STRING: LI| = STRING: Vi| = 
STRING: Vt)" | sed 's/.*\.\([0-9]*\) = STRING:.*/\1/'`
fi

for i in $list
do
        index=$i
        type=`snmpget -v 2c -c $community -Oev $host 1.3.6.1.2.1.2.2.1.2.
$index | sed 's/^STRING: //'`
        description=`snmpget -v 2c -c $community -Oev $host 
1.3.6.1.2.1.31.1.1.1.18.$index | sed 's/^STRING: //'`
        status=`snmpget -v 2c -c $community -Ov $host .1.3.6.1.2.1.2.2.1.7.
$index | sed 's/^INTEGER: \(.*\)(.)/\1/'`
        protocol=`snmpget -v 2c -c $community -Ov $host .1.3.6.1.2.1.2.2.1.8.
$index | sed 's/^INTEGER: \(.*\)(.)/\1/'`

        if [ $output ]
        then
                echo -e  "$type\t$status\t$protocol\t$description" >>$output
        else
                echo -e  "$type\t$status\t$protocol\t$description"
        fi

done

-- 

----------------------

Brian Raaen
Network Engineer
braaen at zcorum.com


On Tuesday 17 November 2009, sky vader wrote:
> Hi,
> 
> Can anyone point me in right direction for a perl script that will
> snmpwalk the MIB for switch port status whether "up" or "down" including
> total number of ports available?
> 
> I have approximately 400 switches that I would like to query via script
> and pipe the results to a file for every device.
> 
> I'm currently querying it manually (see below) which is not scaling :-)
> 
> $ snmpwalk -c <community> <host_ip>
> interfaces.ifTable.ifEntry.ifOperStatus | grep down
> 
> 
> Any pointers will be greatly appreciated.
> 
> 
> regards
> sky
> 
> _______________________________________________
> cisco-nsp mailing list  cisco-nsp at puck.nether.net
> https://puck.nether.net/mailman/listinfo/cisco-nsp
> archive at http://puck.nether.net/pipermail/cisco-nsp/
> 



More information about the cisco-nsp mailing list