Plan 9 from Bell Labs’s /usr/web/sources/contrib/sl/rc/wifiroam

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc
#
# wifiroam [-r] essid
#
# switch bssids with a given essid randomly or
# re-associate with the same bssid
# until ping to the gateway is stable
#

rfork en

fn usage {
	echo usage: /bin/wifiroam [-r] essid >[1=2]
	exit usage
}

r=()
while(~ $1 -*){
	switch($1){
	case -r
		r=1
		shift
	case -*
		usage
	}
}
if(~ $#* 0)
	usage

path=(/bin)
builtin cd /

net=/net/ether0/
myap=$1
#mygate=`{ndb/query sys $sysname ipgw}
mygate=8.8.8.8

ramfs
builtin cd /tmp
cp /bin/ndb/query /tmp
cp /bin/ip/ping /tmp
cp /bin/grep /tmp
cp /bin/awk /tmp
cp /bin/rc /tmp
cp /bin/sleep /tmp
cp /bin/echo /tmp
cp /bin/fortune /tmp
cp /bin/test /tmp
cp /bin/hoc /tmp
cp /bin/wc /tmp

bind /tmp /rc
bind /tmp /bin

fn sigint {
	exit $status
}

fn pinggate {
	pcount=$1
	plost=`{ping -n $pcount -i 200 -l -w 1000 $mygate | grep '^lost' | wc -l}
	echo $plost^' / '^$pcount^' * 100' | hoc
}

fn nowap {
	grep bssid: $net/ifstats | awk '{ print $2 }'
}

fn nextap {
	fortune <{grep node: $net/ifstats | grep $myap | grep -v `{nowap} | awk '{ print $2 }' }
}

fn switchap {
	echo bssid $1 > $net/clone
}


fn x {
	lossy=0

	echo connected to `{grep bssid: $net/ifstats | awk '{ print $2 }' }

	while(sleep 1){
		lost=`{pinggate 10}
		if(test $lost -gt 50){
			lossy=1
			next=`{nextap}
			if(! ~ $r 1 && ! ~ $#next 0){
				echo lost $lost%, switching to $next...
			}
			if not {
				echo lost $lost%, retrying `{nowap}
				next=`{nowap}
			}
			switchap $next
		}
		if not{
			if(~ $lossy 1){
				lossy=0
				echo connected to `{grep bssid: $net/ifstats | awk '{ print $2 }' }
			}
		}
	}
}

x



Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.