wlan autoconnection skripts
[LoN]Kamikaze
LoN_Kamikaze at gmx.de
Fri Aug 26 16:12:27 GMT 2005
I made a little script to autmatically connect to known wireless
networks. I'd like to have some people help me with testing. I'm running
it on FreeBSD 5.4 with the ipw driver.
If it works satisfactory for everyone I'd like to write a manpage and
maybe turn it into a port.
INSTALLATION
Put the attached files somewhere and cd into that folder. Don't forget
to become root.
# cp wi.sh /usr/local/etc/rc.d/
# cp wi /usr/local/sbin/
# mkdir /etc/wi.conf
# chmod 0700 /etc/wi.conf
BASIC CONFIGURATION
You require the following entries in your /etc/rc.conf file to use the
script.
wi_enable="YES"
wi_device="devicename"
Further there are 2 optional entries:
wi_module="kernelMod"
wi_loadfirmware="command"
wi_enable enables the script. wi_device is the name of the wireless
network device as seen by ifconfig. wi_module is optional and the name
of the kernel module for the wireless device. wi_loadfirmware is for
wireless devices that need to load a firmware after the kernel module
has been loaded. As an example here is an excerpt from my rc.conf.
wi_enable="YES"
wi_device="ipw0"
wi_module="if_ipw"
wi_loadfirmware="/usr/sbin/ipwcontrol -i ipw0 -f
/usr/local/libdata/if_ipw/ipw2100-1.3.fw"
Now you have the commands
/usr/local/etc/rc.d/wi.sh start
/usr/local/etc/rc.d/wi.sh stop
/usr/local/etc/rc.d/wi.sh status
available. Or if you want it short
wi start
wi stop
wi status
The following step is optional:
I added the command
"/usr/local/etc/rc.d/wi.sh stop"
at the appropriate places in the files /etc/rc.suspend and
/etc/rc.shutdown .
To /etc/rc.resume I added the command
"/usr/loca/etc/rc.d/wi.sh start".
CONFIGURATION OF NETWORKS
If you did everything described above the script will be able to look
for networks. Unfortunately it won't connect to any... that is what the
folder /etc/wi.conf/ is meant for.
Simply add a file with the name of the network you want to connect to.
An example for such a file would be
MYNET
/sbin/ifconfig ipw0 ssid MYNET wepkey MYKEY weptxkey 1 wepmode on
/sbin/dhclient ipw0
Optionally you can add the file MYNET_stop and enter commands that
should be executed before the script tries to terminate a connection. In
most cases this won't be necessary. The script will automatically
terminate the dhcp client, bring the device down and unload the kernel
module.
Thank you for your time.
Dominic Fandrey
-------------- next part --------------
#!/bin/sh
# Author: kamikaze
# Contact: LoN_Kamikaze at gmx.de
#
# Please read the comments in /usr/local/etc/rc.d/wi.sh if you
# require documentation.
#
# The desired action.
command=$1
# The error number to return.
error=0
# The characters wireless network names may consist of.
netname_chars="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"
# The path for the network init scripts.
network_scripts="/etc/wi.conf"
# Network profile.
network_profile="/var/run/wi"
# Scan results.
network_scan="/var/run/networks"
# Default settings.
wi_enable="NO"
wi_module=""
wi_device=""
wi_loadfirmware=""
# Get settings from rc.conf.
. /etc/rc.subr
name="wi"
rcvar=`set_rcvar`
load_rc_config $name
# Check if the wi service is enabled.
if [ "$wi_enable" = "NO" ]; then
echo "wi_$command: The wi service is not enabled."
exit 1
fi
# Check if a device has been defined.
if [ "$wi_device" = "" ]; then
echo "wi_$command: No device has been configured for the wiinit service."
exit 1
fi
wi_start()
{
if [ "$wi_module" ]; then
# Check if the wireless device module is loaded.
/sbin/kldstat -n $wi_module 2> /dev/null > /dev/null
if [ $? -eq 1 ]; then
echo "wi_$command: Load kernel module for wireless network device."
/sbin/kldload $wi_module 2> /dev/null > /dev/null
$wi_loadfirmware
fi
fi
# Find networks.
echo "wi_$command: Searching for wireless networks."
/sbin/ifconfig $wi_device up
/usr/sbin/wicontrol $wi_device -L > /dev/null
networks=$(/usr/sbin/wicontrol $wi_device -l | grep netname | grep -Eo "[$netname_chars]+" | grep -v SSID | grep -v netname)
# Save scan results.
echo "$networks" > $network_scan
# Practically wicontrol returns networks
# in the order of their signal quality.
for network in $networks; do
script=$(cat "$network_scripts/$network" 2> /dev/null)
if [ $? -eq 0 ]; then
echo "wi_$command: Run init script for $network."
echo "$script" | /bin/sh
if [ $? -eq 1 ]; then
echo "wi_$command: Init script $network failed."
continue
else
echo "wi_$command: Done."
echo $network > $network_profile
exit $error
fi
fi
done
# Check weather any networks have been found.
if [ "$networks" = "" ]; then
echo "wi_$command: No wireless networks have been found."
fi
# Finding a network did not succeed.
error=1
eval wi_stop
}
wi_stop()
{
# Get the current profile.
network=$(cat $network_profile 2> /dev/null)
if [ $? -eq 0 ]; then
script=$(cat $network_scripts/$network_stop 2> /dev/null)
if [ $? -eq 0 ]; then
echo "wi_$command: Run deactivation script for $network."
echo "$script" | /bin/sh
fi
fi
echo "wi_$command: Terminate dhclient sessions on $wi_device."
/sbin/dhclient -r $wi_device
# Check if the device is running and shut it down.
/sbin/ifconfig $wi_device | grep UP | grep RUNNING 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "wi_$command: Shuting down $wi_device."
/sbin/ifconfig $wi_device down
fi
# Check if the kernel module is loaded and unload it.
/sbin/kldstat -n $wi_module 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "wi_$command: Unloading $wi_module kernel module."
/sbin/kldunload $wi_module
fi
# Remove the profile record.
/bin/rm $network_profile 2> /dev/null > /dev/null
# Terminate program.
exit $ERROR
}
wi_status()
{
# Check weather a wireless network is configured.
network=$(cat /var/run/wi 2> /dev/null)
if [ $? -eq 1 ]; then
echo "wi_$command: Not connected."
else
echo "wi_$command: profile: $network"
status=$(/sbin/ifconfig $wi_device | grep status)
echo "wi_$command:$status"
fi
echo "wi_$command: Available networks:"
cat $network_scan 2> /dev/null
exit $error
}
# Run desired command.
wi_$command
More information about the freebsd-mobile
mailing list