Search code examples
linuxsnmp

Replace SNMP "Timeout: No Response from <IP_Address>" with Specific Text


I have a script that queries OID details via SNMPWalk, the problem is when the certain IP (example is 172.20.36.8) is not reachable, it displays Timeout: No Response from 172.20.36.8. Is there a code that could replace that response from snmp query so that if it displays like that, I would like to appear the word "Idle" instead of that "Timeout:..."

here is the part of the script that I am running:

for EACH in `echo $APIP`
  do
    SectorID=`$SNMPWALK -v2c -c Canopy ${EACH} ${SNMPOID}.1.1.16 | awk '{print $4}'
    ActiveSubs=`$SNMPWALK -v2c -c Canopy ${EACH} ${SNMPOID}.1.7.1 | awk '{print $4}'
    UniqueSubs=`$SNMPWALK -v2c -c Canopy ${EACH} ${SNMPOID}.1.7.18 | awk '{print $4}'
    printf "%s\t| %s\t| %s\t| %s\t|\n" "${EACH}" "${SectorID}" "${ActiveSubs}" "${UniqueSubs}"
  done

Now here is the result of the said part of script

172.20.36.3     |   1   |      2        |      4        |
172.20.36.4     |   2   |      5        |      8        |
172.20.36.5     |   3   |      11       |      16       |
Timeout: No Response from 172.20.36.6
Timeout: No Response from 172.20.36.6
Timeout: No Response from 172.20.36.6
172.20.36.6     |       |               |      0        |
172.20.36.7     |   5   |      0        |      1        |
Timeout: No Response from 172.20.36.8
Timeout: No Response from 172.20.36.8
Timeout: No Response from 172.20.36.8
172.20.36.8     |       |               |      0        |

when it reaches the ip address .6 and .8, it displays "Timeout: No Response...". I wanted somehow to display it like this if it encounters no response from the said ip address:

172.20.36.3     |   1   |      2        |      4        |
172.20.36.4     |   2   |      5        |      8        |
172.20.36.5     |   3   |      11       |      16       |
172.20.36.6     |  Idle |     Idle      |     Idle      |
172.20.36.7     |   5   |      0        |      1        |
172.20.36.8     |  Idle |     Idle      |     Idle      |

can anyone out there that can help me to resolve this? I greatly appreciate your responses. :)


Solution

  • You should be able to do something like this:

    SectorID=`$SNMPWALK -v2c -c Canopy ${EACH} ${SNMPOID}.1.1.16 2>&1 | sed 's/Timeout: No Response.*/Idle/'`
    if [ "$SectorId" != "Idle" ] ; then
        SectorID=`echo $SectorID | awk '{ print $4 }'`
    fi
    

    But, the complexity of that script is growing rapidly and I'd be much more tempted to rewrite it in a better scripting environment than sh (I'm not bashing bash; I use it all the time) like python or perl. But if you want to use *sh, I'd at least implement a function to do the processing for you once rather than repeating the above 4 times. But I'll leave that as an exercise for the reader.