码迷,mamicode.com
首页 > 系统相关 > 详细

ubuntu下wifi连接无线路由的脚本

时间:2015-08-27 11:06:19      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:wi-fi   路由器   脚本   ubuntu   wpa_supplicant   

ubuntu下wifi连接无线路由的脚本


主要是使用命令行下,控制wifi接口连接无线路由器,写了一个简单的脚本,仅供参考!


leekwen@print:~$ cat wifi-link.sh
#!/bin/sh
#
# author leekwen
#
read -p "Enter Wifi SSID Name: " ssid

echo "\n \033[32m The WIFI SSID is $ssid. \033[0m \n"

read -p "Enter the password of SSID:" password
echo "\n \033[32m The Password is $password \033[0m\n"

wpa_passphrase $ssid $password > /etc/wpa_supplicant.conf


wpa=`ps -ef |grep wpa_supplicant|grep -v "grep" |wc -l`
dhc=`ps -ef |grep "dhclient wlan0" |grep -v "grep" |wc -l`

#echo "dhclient is $dhc "

pid=`pgrep wpa_supplicant`

cid=`ps -ef |grep "dhclient wlan0" |grep -v "grep" |awk '{print $2}'`

#echo $cid

if [ $wpa -eq 0 ]; then
        echo " process is none "
        wpa_supplicant -B -Dwext -iwlan0 -c /etc/wpa_supplicant.conf &

        if [ $cid -eq 0 ]; then
                dhclient wlan0 &
        else
                kill $cid
                sleep 1;
                dhclient wlan0 &
        fi
else
        echo "\n \033[31m  The process is exists...... \033[0m \n"
        #echo $pid

        kill $pid &

        sleep 1

        echo "\n \033[31m  The interface will restart...... \033[0m \n"

        wpa_supplicant -B -Dwext -iwlan0 -c /etc/wpa_supplicant.conf &
                if [ $cid -eq 0 ]; then
                        dhclient wlan0 &
                else
                        kill $cid
                        sleep 1;
                        dhclient wlan0 &
                fi

fi

根据提示输入wifi的SSID名称及密码后,WIFI接口会自动重启,并从路由上动态获取IP地址。

如果wifi的接口名非wlan0,请自行修改此脚本,以方便自己能够正常使用。

版权声明:本文为博主原创文章,未经博主允许不得转载。

ubuntu下wifi连接无线路由的脚本

标签:wi-fi   路由器   脚本   ubuntu   wpa_supplicant   

原文地址:http://blog.csdn.net/leekwen/article/details/48023321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!