#!/bin/bash ######################################################################################################## # Tripplite Fencing Script (BASH) # Eric Knific (iggi) # eknific@etshost.com # www.etshost.com / www.ericknific.com / iggi.me # # # Usage: ./Tripplite_Fence # # Expected Responses: # # Sucessful Reboot: # Turning off ports 1,2 # Port Status: 1: Down 2: Down # Confirmed Shutdown # Turning on ports 1,2 # Port Status: 1: Up 2: Up # Confirmed Startup # # # ######################################################################################################## snmp_switch(){ for (( k=1; k < $outlets; k++ )); do snmpset -v 2c -c ${arg[1]} ${arg[0]} ${OIDcontrol}${outlet[$k]} i $action done } snmp_check(){ result=`snmpget -v 2c -c ${arg[1]} ${arg[0]} ${OIDstatus}${outlet[$k]}` #Save result into a variable IFS=' ' read -ra resultarr <<< "$result" #Parse the result to an array (space delimited) if [[ ${resultarr[3]} = "1" ]]; #If the port is already off, then we are done then echo "Port is off" else echo "Port is on" fi } args=$# ((args++)) arg=("$@") OIDcontrol=".1.3.6.1.4.1.850.100.1.10.2.1.4." OIDstatus=".1.3.6.1.4.1.850.100.1.10.2.1.2." if [[ ${args} < "3" ]]; then echo 'Usage: ./Tripplite_Fence ' exit 1 fi j=1 for (( i=3; i<${args}; i++)); do outlet[$j]=${arg[$i]} ((j++)) done outlets=${#outlet[@]} if [[ ${arg[2]} = "off" ]]; then action=1 result=0 echo "Turning Ports off" snmp_switch echo "Checking they are off" for (( k=1; k < $outlets; k++ )); #Do this for all outlets do while true; do snmp_check if [[ ${resultarr[3]} = "2" ]]; then echo "Port not off, trying again in 1s" sleep 1 else break fi done done fi if [[ ${arg[2]} = "on" ]]; then action=2 echo "Turning Ports on" snmp_switch echo "Checking they are on" for (( k=1; k < $outlets; k++ )); #Do this for all outlets do while true; do snmp_check if [[ ${resultarr[3]} = "1" ]]; then echo "Port not on, trying again in 1s" sleep 1 else break fi done done fi if [[ ${arg[2]} = "reboot" ]]; then action=1 result=0 echo "Turning Ports off" snmp_switch echo "Checking they are off" for (( k=1; k < $outlets; k++ )); #Do this for all outlets do while true; do snmp_check if [[ ${resultarr[3]} = "2" ]]; then echo "Port not off, trying again in 1s" sleep 1 else break fi done done #Now turn them back on action=2 echo "Turning Ports on" snmp_switch echo "Checking they are on" for (( k=1; k < $outlets; k++ )); #Do this for all outlets do while true; do snmp_check if [[ ${resultarr[3]} = "1" ]]; then echo "Port not on, trying again in 1s" sleep 1 else break fi done done fi