- 論壇徽章:
- 1
|
呵... 先說一下 script 的產(chǎn)生背景吧.
因?yàn)槲壹业母窬质桥_灣傳統(tǒng)的透天厝,
AP 通常只能覆蓋單一樓層, 因此我在 1,2,3 樓各有一個(gè) AP , 分別設(shè)為不同的 essid .
我試過設(shè)定為相同的 essid, 但有時(shí)會(huì)產(chǎn)生"蓋臺"現(xiàn)像: 也就是在 2 樓會(huì)抓到 3 樓的 AP .
呵... 別問我為甚麼會(huì)這樣, 我對 wlan 沒啥研究.... (其實(shí)是偷懶... ^_^)
anyway, 這在之前用 XP 的時(shí)候沒啥問題(呵, 我現(xiàn)在用 SuSE 啦...).
因?yàn)?XP 那邊可以自動(dòng)抓取各層的 essid , 只要將優(yōu)先次順調(diào)好就行了.
但自從改用 SuSE 之後, 就有點(diǎn)不那麼方便了, 每次換樓層都要再設(shè)一次 essid.
而且, 有時(shí)帶著 notebook 到上班的地方去, 也都要再設(shè)一次. 真的很不方便.
呵... 別叫我用那個(gè) yast 啦, 我的耐性還沒那麼好修養(yǎng)... >_<
唉... 也別嫌我笨, 我真的找不到 SuSE 哪里有自動(dòng)設(shè)定 wlan 的工具.
於是, 山不轉(zhuǎn)路轉(zhuǎn), 操起 shell script , 自己寫一個(gè)吧! 呵~~~
得了得了, 我知道你要說甚麼...
好吧, 我得承認(rèn)這不是一個(gè)終極方法.
而且, 我也得坦白: 其實(shí)也不是絕對自動(dòng)啦,
要用到 encription 跟 authentication 時(shí), 還是要先將 profile 寫好的.
當(dāng)然囉, 這個(gè) script 只是自己在用, 你要想"偷"回去的話, 也未必 work ,
起碼你得懂得一些 shell script 技巧才改得動(dòng).
不管啦, 反正, 現(xiàn)在我用得好好的: 換樓層自動(dòng)會(huì)設(shè)好... ^_^
只要寫一個(gè)如下那樣的 shell script,
再為常用那幾個(gè) AP 分別寫好 profile (內(nèi)容是從 SuSE 的 ifcfg-wlan 抄來再改的) 放到同一個(gè)目錄.
然後, 再修改 crontab, 每隔一個(gè)時(shí)間來重跑就行了. (我是每分鐘跑一次啦)
第一版的 script 內(nèi)容如下(多多指教哦):
- #!/bin/bash
- # script name: setwlan.sh
- # purpose: set wlan in Linux
- # author: nemtan<netman@study-area.org>
- # license: GPL
- # version: v.01
- # caveat: Only tested on SuSE pro 9.3
- # change log
- # v.01 (2005-10-15)
- # - first release
- PATH=/sbin:/bin:/usr/sbin:/usr/bin
- # set favorite ap(s) in order
- ap_order="Asus 3Com SMC"
- # set gateway if necessary
- #gw=192.168.1.1
- # set destination for ping test
- #dest=168.95.1.1
- # get current essid
- c_id=`iwconfig 2>/dev/null | grep ESSID | awk -F\" '{print $2}'`
- # get wlan interface
- w_if=`iwconfig 2>/dev/null | grep ESSID | awk '{print $1}'`
- # get current ip
- c_ip=`ifconfig $w_if | awk '/inet addr:/{print $2}' | cut -d: -f2`
- # get crrent gw
- c_gw=`route -n | awk '/^0\.0\.0\.0/{print $2}' | head -1`
- # set path of 'ifcfg-' file
- ifc_file=/etc/sysconfig/network/ifcfg-wlan-id-*
- # set profile dir & prefix
- # profile name should look like: profile.essid
- pro_dir=${0%/*}
- pro_prefix=profile
- # function to bring up interface and gateway
- setwlan() {
- essid=$1
- profile=$pro_dir/$pro_prefix.$essid
- echo "Trying $essid, please wait..."
- # use profile if exists
- if [ -r "$profile" ]; then
- cat $profile > $ifc_file
- # or just change essid only
- else
- sed -i -e "/^WIRELESS_ESSID=/s/=.*/='$essid'/" $ifc_file
- fi
- # re-activate interface
- ifdown $w_if &>/dev/null
- sleep 0.5
- ifup $w_if 2>/dev/null
- # set gateway if specified
- [ "$gw" ] && route add default gw $gw 2>/dev/null
- # show route table
- sleep 0.5
- route -n
- # test connectivity
- [ "$dest" ] && { ping -c2 -w3 $dest || return 1 ; }
- return 0
- }
- # run program if essid has been specified in parameter
- [ "$1" ] && setwlan $1 && exit 0
- # otherwise to search all essid avaliable
- all_essid=`iwlist scanning 2>/dev/null | grep ESSID | awk -F\" '{print $2}'`
- # set essid based on ap list order
- for i in $ap_order
- do
- echo $all_essid | egrep -qw $i && {
- # nothing to do if essid was up
- [ "$i" = "$c_id" -a "$w_if" -a "$c_gw" ] && exit 1
- # or activate it
- setwlan $i && exit 0
- }
- done
- # else for other essid(s) which is not in the ap list
- for i in $all_essid
- do
- setwlan $i && exit 0
- done
復(fù)制代碼
[ 本帖最后由 網(wǎng)中人 于 2005-11-26 23:43 編輯 ] |
|