You are not logged in.

#1 2010-04-25 13:05:27

xelados
Member
Registered: 2007-06-02
Posts: 314
Website

WLAN toggling script

While looking for ways to optimize the battery life of my Samsung NC10, I realized that I really didn't need the network at boot.

"It'd be great to have a quick way to toggle the network without having to enable and re-enable the ath5k module each time," I thought. So, wlan was born:

#!/bin/bash
# wlan - used to toggle a wireless network connection
# by Daniel Campbell <danny@sporkbox.us>
#
# NOTE: This script requires sudo access to the following:
#    /sbin/modprobe
#    /usr/bin/netcfg
# 
# These are used in order to enable and disable the kernel module (to save
# power) and use netcfg as the application to connect.
#
# Replace the OpenNTPD lines with any network-aware applications you want to
# use as soon as you connect.

# Is a netcfg profile already active? If so, it will return the name
# of the connected profile. We can use this to decide what to do.
status=$(netcfg current)

function net_up() {
    # -z checks if the value supplied is longer than 0. If so, it
    # returns 'true'.
    if [ -z $status ]; then
        if [ $1 ]
        then
            # Run all the commands we need in order to connect
            sudo modprobe ath5k
            sudo netcfg $1

            # This includes any daemons we might want to run as soon
            # as we connect.
            sudo /etc/rc.d/openntpd start
        else
            echo "You must supply a netcfg profile to load."
        fi
    fi
}

function net_down() {
    # -n checks to see if a value is empty. If so, it returns 'true'.
    if [ -n $status ]; then
        # This block should be the reverse of what you have in
        # net_up.

        # First, any daemons we needed are no longer needed.
        sudo /etc/rc.d/openntpd stop

        # Then we disable the network itself.
        sudo netcfg -a
        sudo rmmod ath5k
    fi
}

function show_help() {
    echo "USAGE:";
    echo "    wlan up \$profile"
    echo "        Connect to the network, where \$profile is the netcfg"
    echo "        profile you want to connect to."
    echo
    echo "    wlan down"
    echo "        Disconnects from the network."
    echo
    echo "AUTHOR:"
    echo "    Daniel Campbell <danny@sporkbox.us>"
    echo
}

if [ $1 ]
then
    case "$1" in
        up) net_up $2; exit 0;;
        down) net_down; exit 0;;
        *) show_help; exit 0;;
    esac
else
    show_help;
fi

Using it is easy. Say your netcfg profile is HOME or something. Just use `wlan up HOME` to connect, and `wlan down` to disconnect. The script loads and unloads the module as necessary.

One possible flaw in my script is its reliance on sudo to work with the module and start or stop daemons. I couldn't think of a better way to get the job done. I hope it's a good enough script for others to use.

Offline

#2 2010-04-25 13:39:56

hokasch
Member
Registered: 2007-09-23
Posts: 1,461

Re: WLAN toggling script

Thanks for sharing! Another easy way if you only use netcfg is this:

cat /etc/network.d/interfaces/wlan0 
PRE_UP="modprobe ath9k"
POST_DOWN="rmmod ath9k"

(global setting for all network-profiles on interface wlan0)
Also rfkill might be worth considering, as radio should be the biggest power drain.

Offline

#3 2010-04-26 02:00:49

xelados
Member
Registered: 2007-06-02
Posts: 314
Website

Re: WLAN toggling script

Radio? I'm not sure I understand. Is rfkill a separate package?

I think one of the problems with the PRE_UP and POST_DOWN methods (which can be done directly in a profile, too) is that it seems only capable for a single command. If you have any network-aware daemons running, you can't use those config variables (to my knowledge) to start/stop them as needed. That was the motivation for creating the script.

Offline

#4 2010-04-26 08:35:40

hokasch
Member
Registered: 2007-09-23
Posts: 1,461

Re: WLAN toggling script

Rfkill is a seperate package, it turns off/on the "radio"-activity (?). But just taking down the module has the same effect, I guess. You can run multiple commands (or execute a script) with "pre/post_up/down" - see here.

Offline

#5 2010-05-07 10:31:09

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: WLAN toggling script

I'm using something similar in my handler.sh:

#!/bin/sh
# /etc/acpi/handler.sh
WLAN=$(find /sys/class/net | grep -o 'wlan[0-9]')
if [[ -z $WLAN ]] || [[ $(echo $WLAN | wc -l) -ge 2 ]];then
  export WLAN=wlan0
fi
EVENT=$1\ $2\ $3\ $4
case "$EVENT" in
    ibm*1005)    # FN+F5
        case "$WLANSTATE" in
            1)    ifconfig $WLAN down;;
            0)    ifconfig $WLAN up;;
        esac;;
esac

Before that i used to have an Openbox Hotkey for XF86WLAN.

Last edited by demian (2010-06-27 22:18:51)


no place like /home
github

Offline

Board footer

Powered by FluxBB