#!/bin/sh # # The AP9606 can accept certain commands via SNMP # # 1.3.6.1.4.1.318.1.1.6.5.1.1.5.1.1.X (X is port number) - 1=on, 3=off # # Copyright (C) 2009 Jon Masters # # Distributed under GNU General Public License version 2. # SNMP_COMMUNITY="private" SNMP_DEVICE="masterswitch.ip.address" SNMP_GET="/usr/bin/snmpget" SNMP_PORT_OID="1.3.6.1.4.1.318.1.1.6.5.1.1.5.1.1" SNMP_SET="/usr/bin/snmpset" TARGETS=("port1" "port2" "port3" "port4" \ "port5" "port6" "port7" "port8") PORTS=("1" "2" "3" "4" "5" "6" "7" "8") TARGET="$1" COMMAND="$2" snmp_get_port_state() { port=$1 state="$($SNMP_GET -v 1 -c $SNMP_COMMUNITY $SNMP_DEVICE \ $SNMP_PORT_OID.$port | sed -nre 's:.*INTEGER\: ([0-9]):\1:p')" if [ "x1" == "x$state" ] then return 1 fi return 0 } snmp_set_port_state() { port=$1 state=$2 if [ "xon" == "x$state" ] then $SNMP_SET -v 1 -c $SNMP_COMMUNITY $SNMP_DEVICE \ $SNMP_PORT_OID.$port i 1 >/dev/null 2>&1 else if [ "xoff" == "x$state" ] then $SNMP_SET -v 1 -c $SNMP_COMMUNITY $SNMP_DEVICE \ $SNMP_PORT_OID.$port i 3 >/dev/null 2>&1 fi fi } in_array() { haystack=( "$@" ) haystack_size=( "${#haystack[@]}" ) needle=${haystack[$((${haystack_size}-1))]} for ((i=0;i<$(($haystack_size-1));i++)); do h=${haystack[${i}]}; [ "x$h" == "x$needle" ] && return $i done return 255 } if [ "x$TARGET" == "xstatus" ] && [ "x$COMMAND" == "x" ]; then for TARGET in ${TARGETS[@]}; do in_array "${TARGETS[@]}" "$TARGET"; item=$? PORT="${PORTS[$item]}" echo -n "$TARGET: " if ! $(snmp_get_port_state $PORT) then echo "on" else echo "off" fi done exit 0 fi if [ "x$TARGET" == "x" ] || [ "x$COMMAND" == "x" ]; then echo "Usage: ippower | " echo "" echo "TARGETS: ${TARGETS[@]}" echo "COMMANDS: on off status" echo "" exit 1 fi in_array "${TARGETS[@]}" "$TARGET"; item=$? if [ 255 -ne $item ] then PORT="${PORTS[$item]}" #echo "TARGET: $TARGET" #echo "PORT: $PORT" if [ "xstatus" == "x$COMMAND" ] then if ! $(snmp_get_port_state $PORT) then echo "on" else echo "off" fi elif [ "xon" == "x$COMMAND" ] then if $(snmp_get_port_state $PORT) then snmp_set_port_state $PORT on echo "requested port on" else echo "unchanged" fi elif [ "xoff" == "x$COMMAND" ] then if ! $(snmp_get_port_state $PORT) then snmp_set_port_state $PORT off echo "requested port off" else echo "unchanged" fi fi else echo "Unknown target: $TARGET" fi