From 64f9ff8fd9f33ae7608c134fbcc8bdd932b15c33 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Fri, 6 Jul 2018 18:25:58 +0200 Subject: [PATCH] toggle switch port using snmp --- snmp-port-toggle.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/snmp-port-toggle.sh b/snmp-port-toggle.sh index b7fa9a7..9943ea2 100755 --- a/snmp-port-toggle.sh +++ b/snmp-port-toggle.sh @@ -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 -- 2.20.1