toggle switch port using snmp
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 6 Jul 2018 16:25:58 +0000 (18:25 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 6 Jul 2018 16:25:58 +0000 (18:25 +0200)
snmp-port-toggle.sh

index b7fa9a7..9943ea2 100755 (executable)
@@ -1,6 +1,9 @@
+#!/bin/sh -e
 
-sw=sw-a200
-PORT=19
+sw=$1
+PORT=$2
+
+test -z "$sw" -o -z "$PORT" && echo "Usage: sw-name port" && exit 1
 
 . ./snmp.conf
 
@@ -8,13 +11,14 @@ snmp="snmpget -v 2c -c $COMMUNITY -Cf -Ov -OQ $sw"
 
 port_status() {
 
-echo -n "$sw $PORT "`$snmp IF-MIB::ifName.$PORT IF-MIB::ifAlias.$PORT IF-MIB::ifType.$PORT IF-MIB::ifOperStatus.$PORT IF-MIB::ifHighSpeed.$PORT`
+echo "$sw $PORT "`$snmp IF-MIB::ifName.$PORT IF-MIB::ifAlias.$PORT IF-MIB::ifType.$PORT IF-MIB::ifOperStatus.$PORT IF-MIB::ifHighSpeed.$PORT`
 
 }
 
 port_status
 
-read -p "# Press ENTER to toggle port" wait_for_key
+status=`$snmp IF-MIB::ifOperStatus.$PORT`
+read -p "# Press ENTER to toggle port which is now $status" wait_for_key
 
 if [ "$status" = 'up' ] ; then
 
@@ -25,5 +29,8 @@ else
        snmpset -v1 -c $COMMUNITY $sw IF-MIB::ifAdminStatus.$PORT i 1
 fi
 
-port_status
+echo "# wait for port status change from $status"
+while port_status | tee /dev/stderr | grep $status ; do
+       sleep 1
+done