Nov 28, 2012

Turn Off the wireless connection on the router as soon as there is no one connected

After developing a script to in order to remotely turn On  the wireless on my DD-WRT router, I have thought it would be a good idea if I could turn Off the wireless connection on the router as soon as there is no one connected anymore.

There might be I'm going to forget to turn it off, or maybe to lazy to do it, so anyhow Why not do it automatically?
So I started using the mighty Google and soon found out the answer on the DD-WRT Forum - the whole  credit for what I will be doing next goes to those guys.

The script is no more no less than the next, I have just changed the original echo true > with echo `date -u` >> for troubleshooting purposes:

if [ -e /tmp/wlan_idle_check ] && [ -z `wl assoclist` ]; 
         then wl radio off; rm /tmp/wlan_idle_check; 
elif [ -z `wl assoclist` ]; 
         then echo `date -u` >> /tmp/wlan_idle_check; 
else 
         rm /tmp/wlan_idle_check; fi;  

Next step I just need to go to router's web interface, activate the cron job and place the script preceded by 0,15,30,45 * * * * root
which will make the job run every 15 minutes.


Finally, according to the description available on DD-WRT forum:

- the cron job will be running every 15 minutes;
- If nobody is found online, cron at first writes a temporary file;
- On the next run if still none is there (file exists) it will disable the wlan;
- If someone is online, WLAN will stay enabled.

I have just had the opportunity to test and see it working  }:-)

Nov 21, 2012

Remotely turn On/Off DD-WRT WiFi at wish



It happened lately that I've got more and more concerned about the side effects WiFi radiation might have. I didn't find any relevant study which could clearly point out whether there are such effects or not, however I have decided not to take my chance, but do something.

So, having DD-WRT v24-sp2 running on my wonderful D-Link DIR-320 I have configured the Radio Time Restriction in such way it' starting daily at 18:00 (when normally I should be back from work) and runs until 24:00 (when I should be asleep).

Well, that's a first step, but having some clear disadvantages: wireless it's On every day from 18:00 to 24:00 no matter whether it's being used or not, so it's running when it shouldn't be and nor running when it should (e.g.: Saturday morning I might have some needs to use it).

Another way would be to turn Radio Time Restriction Off and use the button SES / AOSS / EZ-SETUP / WPS Button (located on the right side of the router) to turn the radio On and Off, which requires the following configuration on Services tab:


But that shouldn't do it either since my router serves three apartments, therefore only the people living in the apartment where it is located would have access to it, but not the others. And even for them it wouldn't be that easy since since they would need to climb a chair in order to reach the router which is being positioned a  round 2,5 meters high.

So the question is, is there a clever way one could turn On DD-WRT WiFi at wish and having it closed when not used? Thanks God, it is!
But in order to achieve it, I would need to use my android phone, with Connectbot installed. THE WHOLE CONCEPT SHOULD WORK AS FOLLOWS:
One should be able to use his Android mobile phone as a remote control in order to turn On Wifi. This should not require advanced skills and should be accomplished with as less taps as possible. The wireless should be turned Off automatically when noticing no one is using it anymore.

So first first of all I would need to create a new user to be used for the remote connection:

root@DD-WRT:~# mkdir /opt/tmp/radio

echo "radio:*:401:10:User,,,:/opt/tmp/radio:/opt/opt/tmp/radio/radion.sh" >> /tmp/etc/passwd



Changing the default console for user radio to /opt/tmp/radio/radion.sh  makes sure the script is being executed when radio logs on and the connection is being closed as soon as the script finishes execution. So, it leaves the user no other option, unless executing the script.

Create the .ssh folder user for ssh key authentication:

mkdir /opt/tmp/radio/.ssh

Create the script which will be used to turn On wifi and save it under the name radion.sh in user's home folder.

#!/opt/bin/bash
echo "Welcome! Initializing procedure..."
# check whether wireless is already on
RADIOSTATE=`sudo wl radio`
if [ "${RADIOSTATE}" = "0x0001" ] ; then
        # if wireless off, starting wireless.
        echo "Wireless is:      OFF"
        sleep 1 #wait one second
        echo "Starting wireless"
        #For some unknown reason most run both commands in this order:
        sudo wl up ; sudo wl radio on
        echo "Wireless is:      ON"
        sleep 1  #wait one second
        echo "Bye bye!"
        sleep 2  #wait two seconds
        exit;
        else
        # if wireless on, it might be a good idea to remotely turn it off.
        echo "Wireless is:      ON"
        sleep 1  #wait one second
        echo "Would you like to turn it OFF?"
        echo "y/n (default is y):"
        read TURNOFF
        if [ "${TURNOFF}" = "n" ] ; then
                echo "Wireless will remain:      ON"
                sleep 1  #wait one second
                echo "Nothing to do..."
                sleep 1  #wait one second
                echo "Bye bye!"
                sleep 2  #wait two seconds
                exit;
                else
                echo "Wireless will be turned:      OFF"
                sudo wl radio off
                sleep 1 #wait one second
                echo "Wireless is:      OFF"
                sleep 1  #wait one second
                echo "Bye bye!"
                sleep 2  #wait two seconds
                exit;
        fi
fi


Assign user radio as owner of the script:

root@DD-WRT:/opt/tmp/radio# chown radio radion.sh

Grant him execution rights:

root@DD-WRT:/opt/tmp/radio# chmod 544 radion.sh

In order to be able to execute wl command as user radio, insert the following line into the file /opt/etc/sudoers under ## User privilege specification ##:

radio ALL=NOPASSWD: /usr/sbin/wl



Next step is to perform the configuration on Android mobile phone's side.
Install ConnectBot on your Android phone.(https://play.google.com/store/search?q=connectbot)

Define a new connection for the user radio, such as radio@your_host_address:



Go to Manage Pubkeys >> go to Generate and create a new RSA key, 1024 bits, no password.
Save the key, I have saved mine under the name radio.ppk.
Long press on the key and select  "Load key on start".
Long press on the key and select "Copy public key".

Using ConnectBot application on your phone,  establish a ssh connection to you router using the root user. Once connected create the .ssh directory for the user radio.

root@DD-WRT:mkdir /opt/tmp/radio/.ssh

Create and edit the file storing the authorized keys:

root@DD-WRT: vi /opt/tmp/radio/.ssh/authorized_keys

Press i for vi insert mode.
Paste the public key from your clipboard into the file.
Save the file using the vi sequence esc followed by :x and hit return.

Now one should be able to use the ConnectBot connection previously defined in order to connect and run the script which will automatically turn the wireless On or Off at wish. 





CREDITS:
Many thanks to Andrei for his prompt and good advises by the time my Linux skills were threatening to prevent me from finishing the project.