Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198902
  • 博文数量: 103
  • 博客积分: 5051
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-08 11:07
文章分类

全部博文(103)

文章存档

2011年(1)

2010年(2)

2009年(100)

我的朋友

分类: LINUX

2009-04-29 22:06:56

使用方法:
将公司的密码固化到下面的代码中去
不带参数运行会获取质量最好的一个essid并连接

或跟上参数 ./wlanStart your_essid
或 ./wlanStart your_essid your_key
源码如下(记得修改其中你的密码):

!/bin/sh

eth="eth1"
essid=$1
key=$2

#ipw=`ps -aux 2>/dev/null | grep ipw3945d | grep -v grep | head -1 | awk '{print $1}'`
if [ -f /var/run/ipw3945d.pid ]
then
        :
        echo "Device is startup"
else
        #Startup wireless device
        echo "Device haven't startup,startup it now..."
        /sbin/ipw3945d
        active_flag=`/sbin/iwlist ${eth} scan | wc -l`
        i=0
        while [ $i -le 10 ]
        do
                if [ $active_flag -le 10 ]
                then
                        active_flag=`/sbin/iwlist ${eth} scan | wc -l`
                        sleep 2
                else
                        i=10
                fi
                i=$(($i+1))
        done
        echo "Sleep $i seconds"
fi

if [ $essid ]
then
        :
else
        #Grep essid
        essids=`/sbin/iwlist ${eth} scan 2>/dev/null | grep "Mode:Master" -B 2 | grep ESSID | cut -d '"' -f 2 | xargs -n 100`
        newessid=`echo $essids | cut -d ' ' -f 1`
        ldquality=0
        for curessid in $essids
        do
                quality=`/sbin/iwlist ${eth} scan 2>/dev/null | \
                        grep ESSID:\"$curessid\" -A 10 | \
                        grep -P "Quality=\d+" -o | \
                        awk -F = '{print \$2}'`
                if [ $quality -ge $oldquality ]
                then
                        newessid=$curessid
                        ldquality=$quality
                fi
        done
        essid=$newessid

        if [ $essid ]
        then
                echo "Use default ESSID $essid , quality ${oldquality}/100 "
        else
                echo "Can not found activited ESSID!!"
                echo "Please specify the essid manualy!"
                exit
        fi
fi

if [ $key ]
then
        echo $key
else
        key="公司的无线密码"
fi

/sbin/ifconfig ${eth} down
/sbin/ifconfig ${eth} up
/sbin/iwconfig ${eth} essid $essid
#grep encrytion flag
enc=`/sbin/iwlist ${eth} scan 2>/dev/null | grep "$essid" -A 4 | tail -1 | cut -d : -f 2`

if [ "$enc" == "on" ]
then
        echo "Encrytion on"
        /sbin/iwconfig ${eth} key $key
else
        echo "Encrytion off"
        /sbin/iwconfig ${eth} key off
fi

if [ -f /etc/dhcpc/dhcpcd-${eth}.pid ]
then
        killall dhcpcd
fi
/sbin/dhcpcd ${eth}
阅读(686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~