红联Linux门户
Linux帮助

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

发布时间:2016-04-27 10:30:34来源:linux网站作者:Leekwen

主要是使用命令行下,控制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,请自行修改此脚本,以方便自己能够正常使用。


本文永久更新地址:http://www.linuxdiyf.com/linux/20132.html