#!/bin/sh # # The IP9258 can accept certain commands via a simple HTTP GET # # CMD=GetPower CMD=SetPower # P60-P63 Ports1-4 # # Copyright (C) 2009 Jon Masters # # Distributed under GNU General Public License version 2. # WGET_CMD="wget --quiet -O - " IPPOWER_DEVICE="ip9258.ip.address" IPPOWER_USERNAME="admin" IPPOWER_PASSWORD="12345678" IPPOWER_URL="http://$IPPOWER_USERNAME:$IPPOWER_PASSWORD@$IPPOWER_DEVICE" TARGETS=("perihelion" "apohelion" "power5" "light") PORTS=("P60" "P61" "P62" "P63") TARGET="$1" COMMAND="$2" 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 } port_state() { port="$1" state="$($WGET_CMD "$IPPOWER_URL/Set.cmd?CMD=GetPower" | sed -nre "s:.*$port=([01]).*:\1:p")" if [ "x1" == "x$state" ] then return 1 fi return 0 } port_on() { port="$1" $WGET_CMD "$IPPOWER_URL/Set.cmd?CMD=SetPower+$port=1" >/dev/null } port_off() { port="$1" $WGET_CMD "$IPPOWER_URL/Set.cmd?CMD=SetPower+$port=0" >/dev/null } 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 ! $(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 ! $(port_state $PORT) then echo "on" else echo "off" fi elif [ "xon" == "x$COMMAND" ] then if $(port_state $PORT) then port_on $PORT if $(port_state $PORT) then echo "ERROR: port reports still powered off" exit 1 else echo "on" fi else echo "unchanged" fi elif [ "xoff" == "x$COMMAND" ] then if ! $(port_state $PORT) then port_off $PORT if ! $(port_state $PORT) then echo "ERROR: port reports still powered on" exit 1 else echo "off" fi else echo "unchanged" fi fi else echo "Unknown target: $TARGET" fi