Home > database >  Script to extract switchport status via snmp
Script to extract switchport status via snmp

Time:06-15

Good day, looking for some assistance, still quite new to scripting and don't have a clue where to start with this.

Scenario: I have multiple Cisco switches on which I want to check the port status and alias and only output certain information from the output. So for example I would run snmpwalk -v 2c -c mycommunity myswitchname .1.3.6.1.2.1.2.2.1.8 and will get a list of all ports with their status and then snmpwalk -v 2c -c mycommunity myswitchname .1.3.6.1.2.1.31.1.1.1.18 will give me a list of the interface alias's. These could be quite long lists if its a stack switch setup

Now I need to match the status with the alias and output something like "aliasname is up or aliasname is down" to a file, but also want to exclude empty strings on the alias output to reduce file size. I would also be using a file containing all the switch names instead of having to run the run the snmp command manually on every switch, so would probably need to reference the file to run the command on

Ideally output would look something like:

Switch1 interface "aliasname" is up Switch1 interface "aliasname" is down

etc

Sample output of SNMP commands snmpwalk with 1.3.6.1.2.1.31.1.1.1.18 will produce something like below

IF_MIB::ifAlias.1 = STRING:
IF_MIB::ifAlias.2 = STRING:
IF_MIB::ifAlias.3 = STRING:
IF_MIB::ifAlias.4 = STRING:
IF_MIB::ifAlias.5 = STRING:
IF_MIB::ifAlias.6 = STRING:
IF_MIB::ifAlias.7 = STRING:
IF_MIB::ifAlias.8 = STRING:
IF_MIB::ifAlias.9 = STRING:
IF_MIB::ifAlias.10 = STRING:
IF_MIB::ifAlias.11 = STRING:
IF_MIB::ifAlias.12 = STRING:
IF_MIB::ifAlias.13 = STRING:
IF_MIB::ifAlias.14 = STRING:
IF_MIB::ifAlias.15 = STRING:
IF_MIB::ifAlias.16 = STRING:
IF_MIB::ifAlias.17 = STRING:
IF_MIB::ifAlias.18 = STRING:
IF_MIB::ifAlias.19 = STRING:
IF_MIB::ifAlias.20 = STRING: AP034
IF_MIB::ifAlias.21 = STRING:
IF_MIB::ifAlias.22 = STRING:
IF_MIB::ifAlias.23 = STRING:
IF_MIB::ifAlias.24 = STRING: AP031
IF_MIB::ifAlias.25 = STRING:
IF_MIB::ifAlias.26 = STRING: AP022
IF_MIB::ifAlias.27 = STRING: AP027
IF_MIB::ifAlias.28 = STRING:
IF_MIB::ifAlias.29 = STRING:
IF_MIB::ifAlias.30 = STRING: Time Clock
IF_MIB::ifAlias.31 = STRING:
IF_MIB::ifAlias.32 = STRING:
IF_MIB::ifAlias.33 = STRING:
IF_MIB::ifAlias.34 = STRING: Intercom Office
IF_MIB::ifAlias.35 = STRING:
IF_MIB::ifAlias.36 = STRING: AP018
IF_MIB::ifAlias.37 = STRING:
IF_MIB::ifAlias.38 = STRING: SW002
IF_MIB::ifAlias.39 = STRING:
IF_MIB::ifAlias.40 = STRING:

snmpwalk with .1.3.6.1.2.1.2.2.1.8 will produce below output

IF_MIB::ifOperStatus.1 = INTEGER: down(2) 
IF_MIB::ifOperStatus.2 = INTEGER: down(2) 
IF_MIB::ifOperStatus.3 = INTEGER: up(1)
IF_MIB::ifOperStatus.4 = INTEGER: up(1) 
IF_MIB::ifOperStatus.5 = INTEGER: up(1) 
IF_MIB::ifOperStatus.6 = INTEGER: up(1)
IF_MIB::ifOperStatus.7 = INTEGER: up(1) 
IF_MIB::ifOperStatus.8 = INTEGER: up(1) 
IF_MIB::ifOperStatus.9 = INTEGER: up(1)
IF_MIB::ifOperStatus.10 = INTEGER: up(1) 
IF_MIB::ifOperStatus.11 = INTEGER: up(1) 
IF_MIB::ifOperStatus.12 = INTEGER: up(1)
IF_MIB::ifOperStatus.13 = INTEGER: up(1) 
IF_MIB::ifOperStatus.14 = INTEGER: up(1) 
IF_MIB::ifOperStatus.15 = INTEGER: up(1)
IF_MIB::ifOperStatus.16 = INTEGER: up(1) 
IF_MIB::ifOperStatus.17 = INTEGER: up(1) 
IF_MIB::ifOperStatus.18 = INTEGER: up(1)
IF_MIB::ifOperStatus.19 = INTEGER: up(1) 
IF_MIB::ifOperStatus.20 = INTEGER: up(1) 
IF_MIB::ifOperStatus.21 = INTEGER: up(1)
IF_MIB::ifOperStatus.22 = INTEGER: up(1) 
IF_MIB::ifOperStatus.23 = INTEGER: up(1) 
IF_MIB::ifOperStatus.24 = INTEGER: up(1)
IF_MIB::ifOperStatus.25 = INTEGER: up(1) 
IF_MIB::ifOperStatus.26 = INTEGER: up(1) 
IF_MIB::ifOperStatus.27 = INTEGER: up(1)
IF_MIB::ifOperStatus.28 = INTEGER: up(1) 
IF_MIB::ifOperStatus.29 = INTEGER: up(1) 
IF_MIB::ifOperStatus.30 = INTEGER: up(1)
IF_MIB::ifOperStatus.31 = INTEGER: up(1) 
IF_MIB::ifOperStatus.32 = INTEGER: up(1) 
IF_MIB::ifOperStatus.33 = INTEGER: up(1)
IF_MIB::ifOperStatus.34 = INTEGER: up(1) 
IF_MIB::ifOperStatus.35 = INTEGER: up(1) 
IF_MIB::ifOperStatus.36 = INTEGER: up(1)
IF_MIB::ifOperStatus.37 = INTEGER: up(1) 
IF_MIB::ifOperStatus.38 = INTEGER: up(1) 
IF_MIB::ifOperStatus.39 = INTEGER: up(1)
IF_MIB::ifOperStatus.40 = INTEGER: up(1)

If have tried something simple like just to see the output but it just gives me the names listed and underneath the status

cat $1 | while read line; do
      IFSTATUS=`snmpwalk -v 2c -c mycomunity $line 1.3.6.1.2.1.2.2.1.8 | grep -o '[^ ]*$'`
      IFNAME=`snmpwalk -v 2c -c mycommunity $line 1.3.6.1.2.1.31.1.1.1.18 | grep -o '[^ ]*$'`
      echo " $IFNAME = is $IFSTATUS " done

CodePudding user response:

First, you'll probably want to read the documentation on "Command Options Affecting Output Formatting", because by using -Oq, you can transform this:

$ snmpwalk -v 2c -c public 192.168.1.1 ifoperstatus
IF-MIB::ifOperStatus.1 = INTEGER: up(1)
IF-MIB::ifOperStatus.2 = INTEGER: up(1)
IF-MIB::ifOperStatus.3 = INTEGER: up(1)
IF-MIB::ifOperStatus.4 = INTEGER: up(1)
IF-MIB::ifOperStatus.5 = INTEGER: up(1)
IF-MIB::ifOperStatus.6 = INTEGER: up(1)
IF-MIB::ifOperStatus.7 = INTEGER: up(1)
IF-MIB::ifOperStatus.8 = INTEGER: down(2)
IF-MIB::ifOperStatus.9 = INTEGER: up(1)

Into this:

$ snmpwalk -v 2c -c public -Oq 192.168.1.1 ifoperstatus
IF-MIB::ifOperStatus.1 up
IF-MIB::ifOperStatus.2 up
IF-MIB::ifOperStatus.3 up
IF-MIB::ifOperStatus.4 up
IF-MIB::ifOperStatus.5 up
IF-MIB::ifOperStatus.6 up
IF-MIB::ifOperStatus.7 up
IF-MIB::ifOperStatus.8 down
IF-MIB::ifOperStatus.9 up

Which is much easier to parse.

Next, if you're trying to correlate the alias and status values, you probably want to read them into array variables.

If we combine those ideas, we end up with something like this:

#!/bin/bash

mapfile -t alias < <(
    snmpwalk -v 2c -c public -Oq "$1" ifalias |
        awk '{print $2}'
)

mapfile -t state < <(
    snmpwalk -v 2c -c public -Oq "$1" ifoperstatus |
        awk '{print $2}'
)

The mapfile command is a bash builtin that reads lines from stdin into an array variable.

This gets us two variables, alias, containing interface names, and state, containing interface status. Now well we need is a loop:

# get the length of the interface list
count=${#alias[@]}

# print name and status for each interface
for (( i=0; i<count; i   )); do
    echo "${i}: ${alias[$i]}: ${state[$i]}"
done

Running against my edgerouter, the above script produces:

$ sh getports.sh 192.168.1.1
0: lo: up
1: itf0: up
2: imq0: up
3: Internet: up
4: Local: up
5: Wifi: up
6: Local: up
7: Local: down
8: Local: up

Running against my netgear md-4100, which doesn't have interface aliases, the script produces:

$ sh getports.sh 192.168.1.254
0: : down
1: : down
2: : up
3: : down
4: : down
5: : down
6: : down
7: : down
8: : down
9: : down
10: : up
...
  • Related