树莓派自动连接无线网络脚本

如果第一次登陆到系统后,想要完成无线网络的连接设置,可以通过将下面脚本内容填写到一个文本文档里,然后执行就可以了。
先说说思路:
1. 首先,要检测一下网卡是否好用。所以第一步是判断wlan0是否存在。
2. 获取当前环境中的无线网络的SSID号并且列出来供用户选择。
3. 提供用户选择的交互式界面来让用户可以根据提示输入无线的SSID和密码。
4. 生成网卡配置文件,并且重新启动网络使网卡配置生效。

操作:
登陆系统后在命令行输入:

[Bash shell] 纯文本查看 复制代码
1
[root@yoyopi ~]# vim.tiny autolink.sh

然后输入i 进入插入模式写内容

[Bash shell] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# Auto connect to your AP .
# Edit by yoyojacky
# date 2014-03-27
#
WLAN=`ifconfig wlan0 |grep wlan0 | awk '{print $1}'`
SSID=`iwlist wlan0 scanning | grep -i ssid`
if [ $WLAN = "wlan0" ]; then
echo "wlan0 is ok! "
fi
echo $SSID | xargs -n1
# offer two choices for user.
#
read -p "Please input ssid that you want to login: " APID
read -p "please input $APID 's password:" APPW
#!/bin/bash
# Auto connect to your AP .
# Edit by yoyojacky
# date 2014-03-27
#
WLAN=`ifconfig wlan0 |grep wlan0 | awk '{print $1}'`
SSID=`iwlist wlan0 scanning | grep -i ssid`
if [ $WLAN = "wlan0" ]; then
echo "wlan0 is ok! "
fi
echo $SSID | xargs -n1
# offer two choices for user.
#
read -p "Please input ssid that you want to login: " APID
read -p "please input $APID 's password:" APPW
echo "Your SSID is $APID\n "
echo "Your Password is $APPW\n"
echo "Is it correct?(yes|no)"
while read ANSWER
do
case $ANSWER in
yes|y)
echo "please wait..."
wpa_passphrase $APID $APPW >> /etc/wpa_supplicant/wpa_supplicant.conf && sed
-i '/#psk/s/#//' /etc/wpa_supplicant/wpa_supplicant.conf && shutdown -r +1 &> /dev/null
echo " Your Pi will reboot in 1 minute and finish the IP address setup..."
break
;;
no|n)
echo "please try again! "
;;
*)
echo "Are you lost your mind? "
;;
esac
done

然后保存好后直接执行就可以了

[Bash shell] 纯文本查看 复制代码
1
2
[root@yoyoRPI ~] #chmod +x autolink.sh
[root@yoyoRPI ~] #sh autolink.sh

会提示1分钟后重启,重启好了,IP就ok了。
欢迎大家尝试哦。

未经允许不得转载:网购经验网 » 树莓派自动连接无线网络脚本

赞 (6)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册