Wifi-startstop

From wikipost
Jump to navigation Jump to search

This script uses the 'expect' program to interact with my router to automatically start and stop the Wi-Fi.

To automatically initiate either a start or a stop I have the following crontab entries:

#start wireless at 18:30 during weekdays
30 18 * * 1-5 /root/bin/start-wifi.sh > /dev/null 2>&1
#turn off wireless at 23:00 every day
0   23 * * * /root/bin/stop-wifi.sh > /dev/null 2>&1


Wi-Fi Start

The start-wifi.sh script contains the following lines:

#!/bin/sh

EXPECT=/usr/bin/expect
EXP_SCRIPT=/root/bin/wifi-start.exp
LOG=/dev/shm/expect.log

if $EXPECT -f $EXP_SCRIPT
then
  echo "`date` Wireless Network started by Expect script" >> $LOG
else
  echo "`date` Wireless Network already active" >> $LOG
fi


The wifi-start expect script looks like this:

set timeout 5
set PASS "secretpw"
set ROUTER "http://192.168.0.1/xslt?PAGE=C04&THISPAGE=C01&NEXTPAGE=C04&DEVNAME=wave0"
set OPTIONS "-anonymous"

spawn /usr/bin/links $ROUTER $OPTIONS
expect {
           "2701HGV-W"   {
                    set CONTINUE 1
         } timeout  {
                    exit 1
         }
}

#search for the password field
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

sleep 1
# enter password
send "$PASS"

# jump down to the submit button
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

#press enter
send "\r"

# acknowledge to post form data
#press enter
send "\r"

expect {
           "You are about to enable the following "   {
                    set CONTINUE 1
         } timeout  {
                    exit 1
         }
}

sleep 1

# jump down to the confirm button
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

# press 'enter'
send "\r"

# acknowledge to post form data
#press enter
send "\r"

expect "Refresh"
# press enter to refresh the page
send "\r"

expect "View Network Summary"

exit 0


Wi-Fi Stop

The stop-wifi.sh script contains the following lines:

#!/bin/sh

EXPECT=/usr/bin/expect
EXP_SCRIPT=/root/bin/wifi-stop.exp
LOG=/dev/shm/expect.log

if $EXPECT -f $EXP_SCRIPT
then
  echo "`date` Wireless Network stopped by Expect script" >> $LOG
else
  echo "`date` Wireless Network already disabled" >> $LOG
fi

The wifi-stop expect script looks like this:

set timeout 5
set PASS "secretpw"
set ROUTER "http://192.168.0.1/xslt?PAGE=C04&THISPAGE=C01&NEXTPAGE=C04&DEVNAME=wave0"
set OPTIONS "-anonymous"

spawn /usr/bin/links $ROUTER $OPTIONS
expect {
           "2701HGV-W"   {
                    set CONTINUE 1
         } timeout  {
                    exit 1
         }
}

#search for the password field
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

sleep 1
# enter password
send "$PASS"

# jump down to the submit button
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

#press enter
send "\r"

# acknowledge to post form data
#press enter
send "\r"

expect {
           "You are about to disable the following "   {
                    set CONTINUE 1
         } timeout  {
                    exit 1
         }
}

sleep 1

# jump down to the confirm button
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"
send -- "\033\[B"

# press 'enter'
send "\r"

# acknowledge to post form data
#press enter
send "\r"

expect "Refresh"
# press enter to refresh the page
send "\r"

expect "View Network Summary"

exit 0