
Following script was designed for an OP who wanted to monitor his cisco switch ports status via linux base bash script.
- Created: February, 2016
- Revision: January, 2019
OP Requirements:
- We need a bash script that can acquire ports status of Cisco switch using SNMP query & act accordingly based on the results, example send sms/email etc,
- The script should first check target device network connectivity by ping, if PING NOT responding, Exit,
- If ping OK, then check SNMP status, if SNMP NOT responding, then error report, & Exit,
- If Ping / SNMP responds OK, then check the port status, if port status is NOT UP , then send email/sms alert 1 time until next status change.
Hardware / Software Used in this post:
- Cisco 3750 24 Gigabit Ports Switch
- Ubuntu 12.4 Server Edition
- Bash Script
- SNMP support enabled on Cisco switch to query port status using MIB names
Solution:
I made following script which checks PING/SNMP status, and then Port Status of Cisco 3750 Switch. This is just an example. You can use your own techniques to acquire the same result. This is fully tested and working script. There are many other ways to do the same like using any NMS app like Nagios, or DUDE which have good GUI control so no need to do coding in the dark : )
Surely this contains too much junk or some unwanted sections, so you may want to trim it according to your taste and requirements.
Regard’s
Syed Jahanzaib
Install SNMP MIBS
First we need to make sure that MIB are installed, Do so by
1 2 3 | sudo apt-get install -y snmpapt-get install -y snmp-mibs-downloadersudo download-mibs |
After this , Add SNMP Mibs entry in
1 | /etc/snmp/snmp.conf |
by adding this line
1 | mibs +ALL |
Save & Exit
Now query your switch by following command to see if snmpwalk is working …
1 | root@Radius:/temp# snmpwalk -v1 -c wl 10.0.0.1 IF-MIB::ifOperStatus |
& you should see something line below if SNMP is working …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | IF-MIB::ifOperStatus.1 = INTEGER: up(1)IF-MIB::ifOperStatus.17 = INTEGER: up(1)IF-MIB::ifOperStatus.5182 = INTEGER: down(2)IF-MIB::ifOperStatus.5183 = INTEGER: down(2)IF-MIB::ifOperStatus.5184 = INTEGER: down(2)IF-MIB::ifOperStatus.10601 = INTEGER: up(1)IF-MIB::ifOperStatus.10602 = INTEGER: down(2)IF-MIB::ifOperStatus.10603 = INTEGER: down(2)IF-MIB::ifOperStatus.10604 = INTEGER: down(2)IF-MIB::ifOperStatus.10605 = INTEGER: up(1)IF-MIB::ifOperStatus.10606 = INTEGER: up(1)IF-MIB::ifOperStatus.10607 = INTEGER: up(1)IF-MIB::ifOperStatus.10608 = INTEGER: up(1)IF-MIB::ifOperStatus.10609 = INTEGER: up(1)IF-MIB::ifOperStatus.10610 = INTEGER: up(1)IF-MIB::ifOperStatus.10611 = INTEGER: up(1)IF-MIB::ifOperStatus.10612 = INTEGER: up(1)IF-MIB::ifOperStatus.10613 = INTEGER: up(1)IF-MIB::ifOperStatus.10614 = INTEGER: up(1)IF-MIB::ifOperStatus.10615 = INTEGER: up(1)IF-MIB::ifOperStatus.10616 = INTEGER: up(1)IF-MIB::ifOperStatus.10617 = INTEGER: up(1)IF-MIB::ifOperStatus.10618 = INTEGER: up(1)IF-MIB::ifOperStatus.10619 = INTEGER: up(1)IF-MIB::ifOperStatus.10620 = INTEGER: up(1)IF-MIB::ifOperStatus.10621 = INTEGER: up(1)IF-MIB::ifOperStatus.10622 = INTEGER: up(1)IF-MIB::ifOperStatus.10623 = INTEGER: up(1)IF-MIB::ifOperStatus.10624 = INTEGER: up(1)IF-MIB::ifOperStatus.10625 = INTEGER: down(2)IF-MIB::ifOperStatus.10626 = INTEGER: down(2)IF-MIB::ifOperStatus.10627 = INTEGER: down(2)IF-MIB::ifOperStatus.10628 = INTEGER: down(2)IF-MIB::ifOperStatus.14501 = INTEGER: up(1) |
OR getting UP/DOWN result for particular port (port 10)
1 | snmpwalk -v1 -c wl 10.0.0.1 IF-MIB::ifOperStatus.10610 -Oqv |
Output Result:
up
the Script!
- mkdir /temp
- cd /temp
- touch monitor_sw_port.sh
- chmod +x monitor_sw_port.sh
- nano monitor_sw_port.sh
and paste following, make sure to edit all info accordingly…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #!/bin/bash#set -x# Script to check Cisco Switch Port Status and send alert accordingly# It will first check PING, then SNMP Status, then PORT status & act accordingly# Email: aacable at hotmail dot com / http : // aacable . wordpress . com# 15-Jan-2019HOST="$1"PORT="$2"SNMP="public"DEVNAME="ZAIB_Main_Switch"HOSTNAME=`hostname`TEMP="temp"COMPANY="ZAIB (Pvt) Ltd."DATE=`date`# GMAIL DETAILSGMAILID="MYGMAIL@gmail.com"GMAILPASS="GMAIL_PASS"ADMINMAIL1="aacableAThotmail.com"SENDMAIL="/temp/sendEmail-v1.56/sendEmail"# SMS RELATED and KANNEL INFO# KANNEL SMS Gateway InfoKANNELURL="127.0.0.1:13013"KANNELID="kannel"KANNELPASS="KANNEL_PASS"CELL1="03333021909"PING_ATTEMPTS="2"HOST_PING_STATUS="/$TEMP/$HOST.$PORT.ping"HOST_PORT_STATUS="/$TEMP/$HOST.$PORT.port"LAST_DOWNTIME_HOLDER="/$TEMP/$HOST.$PORT.last_down.status.txt"touch $HOST_PING_STATUStouch $HOST_PORT_STATUStouch $LAST_DOWNTIME_HOLDER# If ip parameters are missing, then inform & exitif [ -z "$HOST" ];thenecho "Error: IP missing, Please use this,./monitor_sw_port.sh 10.0.0.1 10601"exit 1fi# If port parameters are missing, then inform & exitif [ -z "$PORT" ];thenecho "Error: PORT number missing, Please use this,./monitor_sw_port.sh 10.0.0.1 10601"exit 1fi# Test PING to devicecount=$(ping -c $PING_ATTEMPTS $HOST | awk -F, '/received/{print $2*1}')if [ $count -eq 0 ]; thenecho "$HOST $DEVNAME is not responding to PING Attempts, cannot continue without , por disable ping check] !"exit 1elseecho "- PING Result : OK"fi# Test SNMP Result of devicesnmpwalk -v1 -c $SNMP $HOST SNMPv2-MIB::sysDescr.0 > /tmp/$HOST.$PORT.snmp.status.txtif [ ! -f "/tmp/$HOST.$PORT.snmp.status.txt" ]; thenecho "- ALERT: ..... $HOST $DEVNAME is not responding to SNMP Request, Cannot continue without it ... Exit"exit 1elseecho "- SNMP Result : OK"fi# If all OK, then pull Port DescriptionPORT_DERSCRIPTION=`snmpwalk -v1 -c $SNMP $HOST IF-MIB::ifDescr.$PORT -Oqv`# Check if folder exists, if not create one and continueif [ ! -d "/$TEMP" ]; thenechoechoecho "/$TEMP folder not found, Creating it so all ping results should be saved there . . ."mkdir /$TEMPfi### START ACTION################################### CHECK PORT STATUS - for UP #################################CHKPORT=`snmpwalk -v1 -c $SNMP $HOST IF-MIB::ifOperStatus.$PORT -Oqv`#CHKPORT="up"# If Port number does not exists, then inform and exitif [ -z "$CHKPORT" ]; thenecho "ALERT: .... Port number $PORT NOT found on $HOST $DEVNAME , Please check Port Number, Exiting ..."exit 1fi########################################## SMS/EMAIL Messages for PORT UP / DOWN ########################################### Temporary file holder for PORT DOWN/UP storing sms/emailPORT_DOWN_MSG_HOLDER="/$TEMP/$HOST.$PORT.down.msg"PORT_UP_MSG_HOLDER="/$TEMP/$HOST.$PORT.up.msg"echo "ALERT:$DEVNAME $HOST port $PORT $PORT_DESCRIPTION is DOWN @ $DATE$COMPANY" > $PORT_DOWN_MSG_HOLDERecho "INFO:$DEVNAME $HOST port $PORT $PORT_DESCRIPTION is OK @ $DATE!$COMPANY" > $PORT_UP_MSG_HOLDERPORT_DERSCRIPTION=`snmpwalk -v1 -c $SNMP $HOST IF-MIB::ifDescr.$PORT -Oqv`HOST_PORT_DOWN_ALERTONSCREEN="ALERT: .... $HOST $DEVNAME port nummber $PORT $PORT_DERSCRIPTION is DOWN @ $DATE"HOST_PORT_UP_ALERTONSCREEN="INFO: .... $HOST $DEVNAME port nummber $PORT $PORT_DERSCRIPTION is OK @ $DATE"# Check if port is UPif [ "$CHKPORT" = "up" ]; thenecho -e "$HOST_PORT_UP_ALERTONSCREEN"# Check if port isUP and its previous state was DOWN, then send UP sms/emailif [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; thenecho "INFO: This port was previosuly DOWN, and now its UP ,Sending UP SMS 1 time only"# Sending PORT DOWN ALERT via EMAIL$SENDMAIL -u "$HOST_PORT_UP_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$PORT_UP_MSG_HOLDER -o message-content-type=text# Sending PORT DOWN ALERT via SMS using KANNEL SMS Gatewaycat $PORT_UP_MSG_HOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-sed -i "/$HOST/d" "$HOST_PORT_STATUS"fifi##################################### CHECK PORT STATUS - for DOWN ###################################if [ "$CHKPORT" = "down" ]; thenecho "$HOST_PORT_DOWN_ALERTONSCREEN"#check if port staus was previosly UP, then actif [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; thenecho "ALERT: ..... $HOST $DEVNAME port $PORT $PORT_DERSCRIPTION is DOWN. SMS have already been sent."fiif [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 0 ]; thenecho "ALERT: ..... $HOST $DEVNAME port $PORT $PORT_DERSCRIPTION is now down! - SENDING PORT DOWN SMS ..."echo "$HOST" > $HOST_PORT_STATUSecho "SMS Sent FOR $HOST $DEVNAME port $PORT $PORT_DERSCRIPTION DOWN have been sent only 1 time until next status change ..."# Sending PORT DOWN ALERT via EMAIL$SENDMAIL -u "$HOST_PORT_DOWN_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$PORT_DOWN_MSG_HOLDER -o message-content-type=text# Sending PORT UP ALERT via SMScat $PORT_DOWN_MSG_HOLDER | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-fifi##################### SCRIPT ENDS HERE ## SYED JAHANZAIB ##################### |
Usage:
change the IP and port number.
- /temp/monitor_sw_port.sh 10.0.0.1 10610
You can add entry in cron like this
1 2 | # Check for Service remote host port status*/5 * * * * /temp/portmon.sh 10.0.0.1 10610 |
RESULT:
SMS result:
Email Result:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # Monitoring Port # 10 , when port is DOWN ...root@Radius:/temp# ./monitor_sw_port.sh 10.0.0.1 10610- PING Result : OK- SNMP Result : OKALERT: .... 10.0.0.1 WL_Main_Switch port nummber 10610 GigabitEthernet2/0/10 is DOWN @ Tue Jan 15 12:44:45 PKT 2019ALERT: ..... 10.0.0.1 WL_Main_Switch port 10610 GigabitEthernet2/0/10 is DOWN. SMS have already been sent.root@Radius:/temp# ./monitor_sw_port.sh 10.0.0.1 10610- PING Result : OK- SNMP Result : OKALERT: .... 10.0.0.1 WL_Main_Switch port nummber 10610 GigabitEthernet2/0/10 is DOWN @ Tue Jan 15 12:44:51 PKT 2019ALERT: ..... 10.0.0.1 WL_Main_Switch port 10610 GigabitEthernet2/0/10 is DOWN. SMS have already been sent.# Monitoring Port # 10 , when port is UP now ...root@Radius:/temp# ./monitor_sw_port.sh 10.0.0.1 10610- PING Result : OK- SNMP Result : OKINFO: .... 10.0.0.1 WL_Main_Switch port nummber 10610 GigabitEthernet2/0/10 is OK @ Tue Jan 15 12:45:01 PKT 2019INFO: This port was previosuly DOWN, and now its UP ,Sending UP SMS 1 time onlyJan 15 12:45:11 radius sendEmail[18700]: Email was sent successfully!0: Accepted for delivery# Monitoring Port # 10 , when port is working UP ...root@Radius:/temp# ./monitor_sw_port.sh 10.0.0.1 10610- PING Result : OK- SNMP Result : OKINFO: .... 10.0.0.1 WL_Main_Switch port nummber 10610 GigabitEthernet2/0/10 is OK @ Tue Jan 15 12:45:12 PKT 2019 |
