SyntaxHighlighter

2015年5月16日星期六

Custom Sleep for LS-QVL

1. Set linkstation to auto mode.
2. Add crontab to run this script every 2mins
*/2 * * * * /root/custom_sleep.sh

root@ls-qvl:~# cat custom_sleep.sh 
#!/bin/bash

################################################################################
#
# CUSTOM_SLEEP                                               by brumi 2010-07-16
#
################################################################################

# NOTE: please run this script every 2mins by crontab

# This cron-job is intended to be used as replacement for the provided standby-
# mechanism from buffalo. It checks for open connections and only when there are
# no open connections found for a certain period of time it will put the link-
# station into standby.



TIMEOUT=1800        # wait TIMEOUT seconds before going to standby

CONN_FILE=/var/run/connected        # file to store timestamp
LOG_FILE=/var/log/custom_sleep        # log-file
ENABLE_LOG='yes'                # enable logging [yes|no]


# ip addresses to exclude from the connection-check (i.e., local ones)
OWN_IPADDR=`/bin/ipaddr show eth0 | /bin/grep -o -e "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | /bin/sed s/"inet "//`
EXCLUDE_IPADDR=(${OWN_IPADDR} 0.0.0.0 127.0.0.1 239.255.255.250 192.168.88.255)
EXCLUDE_IPADDR_CT=5


# disable sleep in manual mode
auto_pwr_stat=`cat /proc/buffalo/gpio/switch/auto_power`
if [ "$auto_pwr_stat" != "on" ]; then
        if [ $ENABLE_LOG == 'yes' ]; then                           
                echo -n `date` >> $LOG_FILE
                echo ": custom_sleep: not in auto mode, exit" >> $LOG_FILE
        fi
        exit 0
fi


# do nothing if already sleeping
if [ -f /etc/linkstation_standby ]; then
        if [ $ENABLE_LOG == 'yes' ]; then
                echo -n `date` >> $LOG_FILE
                echo ": custom_sleep: already sleeping" >> $LOG_FILE
        fi
        exit 0
fi


# function to check if any clients are connected
connected () {
        CONN_IPADDR=`/bin/netstat -tna 2> /dev/null | /bin/sed s/"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"/"&\n"/ | /bin/grep -o -e "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`
        for ip in $CONN_IPADDR
        do
                exclude=0
                for ((i=0;i<$EXCLUDE_IPADDR_CT;i++))
                do
                        if [ ${EXCLUDE_IPADDR[$i]} == $ip ]; then
                                exclude=1
                        fi
                done
                if [ $exclude -eq 0 ]; then
                        if [ $ENABLE_LOG == 'yes' ]; then
                                echo -n `date` >> $LOG_FILE
                                echo ": custom_sleep: active ip $ip" >> $LOG_FILE
                        fi
                        return 1
                fi
        done
        return 0
}

# check for connections and update $CONN_FILE
connected
if [ $? -eq 1 ]; then 
        if [ -f $CONN_FILE ]; then 
                rm $CONN_FILE
        fi
else
        if [ ! -f $CONN_FILE ]; then
                date +%s > $CONN_FILE        
        fi
fi        


# check when last connection was found
if [ -f $CONN_FILE ]; then 
        last_conn=`/bin/cat $CONN_FILE`
        comp_time=`/bin/date +%s`
        let "comp_time -= $TIMEOUT"
        if [ $last_conn  -gt $comp_time ]; then
                /usr/local/sbin/pwrmgr -c localhost act
                if [ $ENABLE_LOG == 'yes' ]; then
                        echo -n `date` >> $LOG_FILE
                        echo ": custom_sleep: wait-after-connection prevented standby" >> $LOG_FILE
                fi
                exit 0
        fi
else 
        /usr/local/sbin/pwrmgr -c localhost act
        if [ $ENABLE_LOG == 'yes' ]; then
                echo -n `date` >> $LOG_FILE
                echo ": custom_sleep: connections prevented standby" >> $LOG_FILE
        fi
        exit 0
fi

# no one busy -> clear timestamp and STANDBY
if [ $ENABLE_LOG == 'yes' ]; then
        echo -n `date` >> $LOG_FILE
        echo ": custom_sleep: going to standby" >> $LOG_FILE
fi
rm $CONN_FILE
/usr/local/sbin/pwrmgr -u

How to WOL:
1. Python2
#!/usr/bin/env python2
import socket
import time

bcast_addr = '192.168.88.255' # broadcast address for you network
target_mac = "4C:E6:76:E7:xx:xx" # colons are optional, case does not matter

mac = target_mac.replace(':', "")
mac_bytes = "".join([chr(int(mac[i:i+2], 16)) for i in range(0, len(mac), 2)])
WoL_packet = '\xff'*6 + mac_bytes * 16

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

while 1:
    s.sendto(WoL_packet, (bcast_addr, 9))
    print "Sent paket!"
    time.sleep(180)
2. ArchLinux

  • pacman -S wol

3. dd-wrt router
  • Administration -- WOL -- Manual WOL, with port 9
4. Windows

没有评论:

发表评论