Working WiFi switch with scripting

Forum just for Ubuntu chat

Moderators: glibdud, janss

Working WiFi switch with scripting

Postby yodersj » Tue Aug 26, 2008 4:49 am

Caveats:
  • I am a Fedora user, but this should be fairly generic across all distros. I'm able to read most things posted here and translate where necessary (and wouldn't be as far as I am without some of the posts here).
  • These instructions assume you have wifi working and fairly stable. Now you're just ready to use the hardware switch on the laptop so you can save some power drain when not using wifi (my powertop shows ~0.5 difference).

Much of this was gleaned from two scripts, rm_driver.sh and add_driver.sh, that came with the Acer Linpus install as well as the .XHkeys file in the Linpus default 'user' home directory. It's been tweaked to my liking, adjust to suit your situation as needed.

Requirements:
1) A program to trap specific keycodes and execute a specific program when a specific keycode is seen. The Acer Linpus is using xhkeys. I've chosen, for now, to use xhotkeys.
2) Might need rights and knowledge to edit the sudoers file. Depends on how your distro is setup.
3) Knowledge of how to create/save a file to '/usr/local/bin/'. This isn't intended to be a full how-to and you could create the files in a place where your regular user account has rights to do so (ala '~/bin/').
4) Adjust paths to any commands shown to match where they are on your system.

Basic steps:
1) Create the "turn off" and "turn on" scripts in the '/usr/local/bin/' directory. Let's pretend we call them the same as on the Acer Linpus install:
  • "turn off": /usr/local/bin/rm_driver.sh
  • "turn on": /usr/local/bin/add_driver.sh

2) Make them executable:
Code: Select all
sudo chmod 0755 /usr/local/bin/rm_driver.sh
sudo chmod 0755 /usr/local/bin/add_driver.sh


3) Test them by hand in a terminal with your normal account:
Code: Select all
/usr/local/bin/rm_driver.sh
/usr/local/bin/add_driver.sh


4) Debug as necessary. If you have to type in a password when running the 'sudo' command at any time from when you first login, see below for some example ways to setup the sudoers file. You could do a quick and dirty '[user] ALL=(ALL) NOPASSWD: ALL' for debugging purposes only. I don't recommend leaving it that way, though.

5) Once you can run the scripts by hand without any errors and the wifi is correctly stopped and started, you can now setup your chosen "hotkey" program. The keycodes that should be trapped are 234 for the "off" script and 233 for the "on" script. When I setup xhotkeys I:
  • Ran, in a terminal, 'xhotkeys -c' to get the configuration screen.
  • In the configuration screen I clicked on the "Add" button.
  • In the "Add" window I typed the name (for example "Wifi-Off"). Then I clicked on the "Change Hotk" button and moved the wifi switch (in this case I knew wifi was on, so the code generated was for the off command). Lastly in the "Command" box I typed in the full path to the script I wanted executed (again, for the off example of /usr/local/bin/rm_driver.sh).
  • The "Test command" button won't do anything unless the xhotkey daemon is running. You could start this in another terminal window by typing only 'xhotkey'. Leave the window running the daemon open to see if there are any errors when testing.
  • Repeat for the "on" script and trapping the "on" code.

6) Once everything is debugged, locked down, and confirmed working with your "hotkey" program then set it to run when you login to X-Windows and enjoy.

The "turn off" script:
Code: Select all
#!/bin/sh
# Disable the wireless drivers
#
# Requires sudo rights to the following commands
RMOD=/sbin/rmmod
IFCFG=/sbin/ifconfig
WLANCFG=/usr/bin/wlanconfig

# Sudo rights not required for the following commands
SDO=/usr/bin/sudo
LMOD=/sbin/lsmod
GRP=/bin/grep
AWK=/usr/bin/awk

###################
$SDO $IFCFG ath0 down
sleep 1
$SDO $WLANCFG ath0 destroy
# New order
$SDO $RMOD ath_pci
sleep 1
for d in `$LMOD | $GRP ^wlan_ | $AWK '{print $1}'`; do
  $SDO $RMOD $d
done
sleep 1
for d in `$LMOD | $GRP ^ath_ | $AWK '{print $1}'`; do
  $SDO $RMOD $d
done
$SDO $RMOD wlan

exit 0


The "turn on" script:
Code: Select all
#!/bin/sh
# Enable wireless drivers
#
# Requires sudo rights to the following commands
MODP=/sbin/modprobe
SYSCT=/sbin/sysctl
IFCFG=/sbin/ifconfig

# Sudo rights not required for the following commands
SDO=/usr/bin/sudo

###################
# Load the driver
$SDO $MODP ath_pci
# Turn the LED back on
$SDO $SYSCT -q -w dev.wifi0.softled=1
$SDO $SYSCT -q -w dev.wifi0.ledpin=3
# Bring the interface up
$SDO $IFCFG ath0 up

exit 0


Locked down sudoers settings example (replace [user] with the account you login as):
Code: Select all
Cmnd_Alias ATHDIFCFG = /sbin/ifconfig ath0 down
Cmnd_Alias ATHDWLANCFG = /usr/bin/wlanconfig ath0 destroy
Cmnd_Alias ATHDRMODW1 = /sbin/rmmod wlan_*
Cmnd_Alias ATHDRMODA = /sbin/rmmod ath_*
Cmnd_Alias ATHDRMODW2 = /sbin/rmmod wlan
Cmnd_Alias ATHUMODP = /sbin/modprobe ath_pci
Cmnd_Alias ATHUSYSCTL1 = /sbin/sysctl -q -w dev.wifi0.softled\=1
Cmnd_Alias ATHUSYSCTL2 = /sbin/sysctl -q -w dev.wifi0.ledpin\=3
Cmnd_Alias ATHUIFCFG = /sbin/ifconfig ath0 up
[user]   ALL = NOPASSWD: ATHDIFCFG, ATHDWLANCFG, ATHDRMODW1, ATHDRMODA, ATHDRMODW2, ATHUMODP, ATHUSYSCTL1, ATHUSYSCTL2, ATHUIFCFG


Semi-locked down sudoers example settings (replace [user] with the account you login as):
Code: Select all
Cmnd_Alias MODP = /sbin/modprobe
Cmnd_Alias SYSCT = /sbin/sysctl
Cmnd_Alias IFCFG = /sbin/ifconfig
Cmnd_Alias RMOD = /sbin/rmmod
Cmnd_Alias WLANCFG = /usr/bin/wlanconfig
[user]   ALL = NOPASSWD: MODP, SYSCT, IFCFG, RMOD, WLANCFG
yodersj
 
Posts: 72
Joined: Thu Aug 21, 2008 12:40 am
Location: NC

Re: Working WiFi switch with scripting

Postby tomo88 » Tue Aug 26, 2008 11:06 am

Wow thanks, this looks great, I just installed Ubuntu on my A110 and unfortunately the switch is useless and would be real good for power saving purposes.

Unfortunately I've been using Linux for only a week (that is, since I got my aspire) so I wouldn't have a clue on how to do this haha, but i'll try read up and learn how cuz it looks real useful.

Thanks again
tomo88
 
Posts: 27
Joined: Wed Aug 20, 2008 9:33 am

Re: Working WiFi switch with scripting

Postby qball » Tue Aug 26, 2008 11:14 am

The switch always kills the wifi (for me). it also causes the powerusage to drop.
But I agree, this is a nice script.
Now if we could manage to turn on/off the wifi led (left one) on the aspire.
qball
 
Posts: 42
Joined: Thu Aug 14, 2008 9:22 am

Re: Working WiFi switch with scripting

Postby tomo88 » Tue Aug 26, 2008 12:22 pm

so just by flickin it with the stock ubuntu install it kills it? awsome.

when the computer starts up tho, is the wireless already on or still in the current status it was during shutdown (be it on or off)?
tomo88
 
Posts: 27
Joined: Wed Aug 20, 2008 9:33 am

Re: Working WiFi switch with scripting

Postby mvincent99 » Wed Aug 27, 2008 5:43 pm

You indicated that you are a fedora user, Which version of fedora are you using ... I am trying F8 ... any comments on using these scipts.
mvincent99
 
Posts: 1
Joined: Mon Aug 25, 2008 10:27 pm

Re: Working WiFi switch with scripting

Postby yodersj » Thu Aug 28, 2008 12:42 am

F8 w/ KDE. Working on making a custom live cd,
yodersj
 
Posts: 72
Joined: Thu Aug 21, 2008 12:40 am
Location: NC

Re: Working WiFi switch with scripting

Postby spinnekopje » Sat Aug 30, 2008 8:20 am

yodersj wrote:Now you're just ready to use the hardware switch on the laptop so you can save some power drain when not using wifi (my powertop shows ~0.5 difference)


These scripts look very interesting, although I still need to take a look at my sudoers file (it keeps asking a passwd). Can that be something with the autologin I use or did I forget to run a certain command (I just rebooted)?

There is one more thing that would make it very interesting to use: make an OSD notice when switching and let the nm_applet immediatly know that the wireless is switched on/off.
spinnekopje
 
Posts: 83
Joined: Wed Aug 13, 2008 7:40 am

Re: Working WiFi switch with scripting

Postby ags » Sat Aug 30, 2008 7:51 pm

spinnekopje wrote:There is one more thing that would make it very interesting to use: make an OSD notice when switching and let the nm_applet immediatly know that the wireless is switched on/off.


Have a look at this post http://www.aspireoneuser.com/forum/viewtopic.php?f=28&t=2191 which gets the OSD going.

Having looked at this I can say a bit more: The OSD and the wifi control scripts are in the 'linpus-hotkey rpm'. The OSD is controlled by Xhotkeys and a small binary 'hotkey-splash', but the wifi scripts are not. In one of the boot-up rc files the wifi switch is set with 'xsetkeycodes' to 158/159, so somewhere else in the system the keycodes 158 and 159 are trapped and the wifi scripts in the 'linpus-hotkey rpm' must be run.

It would be easy to have the wifi script call the binary 'hotkey-splash' for the OSD. Indeed this was what Linpus originally did, you can see it commented out in either add_driver.sh or rm_driver.sh (can't recall which).

Linpus has the same problem with delay in nm_applet, so I guess they weren't able to fix it either.
ags
 
Posts: 32
Joined: Tue Jul 22, 2008 9:05 am

Re: Working WiFi switch with scripting

Postby spinnekopje » Wed Sep 03, 2008 3:08 pm

ags wrote:Linpus has the same problem with delay in nm_applet, so I guess they weren't able to fix it either.


The Linpus didn't run long enough to test that, but I wonder whether it is possible to control the nm_applet from a script. Otherwise I would like to add a call that does the same as right-click - disable wireless on the nm_applet (or whatever it is called in English, mine is in Dutch).

It seems to me that it should work instantly, certainly if combined with the scripts from this thread... However I didn't find any information about changing options for the nm_applet from the command line.
spinnekopje
 
Posts: 83
Joined: Wed Aug 13, 2008 7:40 am

Re: Working WiFi switch with scripting

Postby ags » Fri Sep 05, 2008 8:51 pm

Thanks for the great script. I've spent several days playing around with it and hopefully made some improvements.

I finally realised that although Linpus have included add_driver.sh and rm_driver.sh in their distribution, they don't actually use them! Also the source rpm's don't match what it is the final computer! Alas, the Linpus scripts are red herrings...

The two scripts are replaced with a single script that accepts a command line option of either 'off' or 'on'.

Code: Select all
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/local/bin

if [ "x$1" = "x" ]; then
  echo "No off|on command given"
  exit 1
fi

case $1 in

  off)
    # Disable the wireless drivers
    # keycode 234

    # basic command : notify-send -t 3000 -u normal "message"
    notify-send -i /usr/share/pixmaps/splash/splash_wifi_off.png \
                --hint=int:'x':340 -h int:'y':200 \
                -u normal -t 1000 " "

    # tell Network-Manager the network is down
    dbus-send --system --type=signal \
              --dest=org.freedesktop.NetworkManager \
              /org/freedesktop/NetworkManager \
              org.freedesktop.NetworkManager.setWirelessEnabled \
              boolean:false

    sleep 1

    wlanconfig ath0 destroy
    rmmod ath_pci

    ;;

  on)

    # Enable wireless drivers
    # keycode 233

    notify-send -i /usr/share/pixmaps/splash/splash_wifi_on.png \                                       
                --hint=int:'x':340 -h int:'y':200 -u normal  \
                -t 1000 " "

    # load the drivers
    modprobe ath_pci
    modprobe wlan_scan_sta

    # turn the LED back on
    sysctl -q -w dev.wifi0.softled=1
    sysctl -q -w dev.wifi0.ledpin=3

    sleep 1

    # tell Network-Manager the network is up
    dbus-send --system --type=signal \
              --dest=org.freedesktop.NetworkManager \
              /org/freedesktop/NetworkManager \
              org.freedesktop.NetworkManager.setWirelessEnabled \
              boolean:true

    ;;

  *)
     echo "Unknown command"
     exit 1
     ;;
esac

exit 0

Now to get this script working you need to apt-get a couple of packages:
Code: Select all
sudo apt-get install libnotify-bin libnotify1

In my case one of these packages was already installed.

You also need to have appropriate graphics installed in the path given in the two libnotify commands. In my case I just used the Linpus graphics as found in the OneLinux package. This gives a nice OSD just like Linpus.

Details of libnotify are in this eeuser forum thread

We also prod the network manager via dbus when the state changes. Finding the D-BUS specification for network manager is not easy, they seem to have gone out of there way to make it hard. A reference is in this gnome mail list message.

The libnotify and dbus-send commands really are all one line!

Finally, the script needs to be run as root. I did this by adding the script name to /etc/sudoers. You should edit sudoers with the command visudo, however this uses the vi editor - which is not for the unfamiliar. An easier approach is to use nano:
Code: Select all
sudo env EDITOR=nano visudo

When saving the file saved will be 'sudoers.tmp' (until visudo checks the syntax).

I added the following:
Code: Select all
Cmnd_Alias WIFI=/usr/local/bin/wifi_switch.sh
asimpson ALL=NOPASSWD: WIFI

The NOPASSWD line must be the last line in the file as commands are read top to bottom, and later commands overwrite the earlier.

Use the switch in your keybindings with either:
sudo wifi_switch.sh off
sudo wifi_switch.sh on
(No password required)

That's it. Enjoy!
ags
 
Posts: 32
Joined: Tue Jul 22, 2008 9:05 am

Next

Return to Ubuntu

Who is online

Users browsing this forum: No registered users and 3 guests