7、设置为插上wifi网卡后自启动
前言:每次接上网卡还要运行那么多的命令是不是很麻烦呢,有没有接上网卡就自己启动的方法呢?当然是有的。下面来实现这个功能。
7.1、功能确认
(1)系统启动后自动加载rt3070驱动
(1)一接入了WIFI网卡,就自动执行wpa_supplicant和wpa_cli
(2)一连接上WIFI AP,就自动执行dhclient
7.2、编辑启动脚本,加上加载驱动的命令
vi /etc/init.d/rcS //在rcS中加入下面一行,这样系统启动后就自动加载驱动:
/@#################
insmod /myprj/rt3070l_usbwifi_driver/rt3070sta.ko
#################@/
7.3、一接入WIFI网卡,就自动执行wpa_supplicant、wpa_cli
这一功能的实现使用的linux的mdev机制,实现自动执行wpa_supplicant
mdev.conf的格式如下:
<device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]
命令说明:
device regex:正则表达式,表示哪一个设备
uid:owner
gid:组ID
octal permissions:以八进制表示的属性
@:创建设备节点之后执行命令
$:删除设备节点之前执行命令
*:创建设备节点之后 和 删除设备节点之前 都执行后面的命令
command:要执行的命令,可以是命令,也可以是一个可执行的脚本。
所以我们编辑我们的mdev配置文件:
vi /etc/mdev.conf //编辑配置文件,加上下面的内容
/@#################配置文件的内容
ra0 0:0 777 * /sbin/auto_wifi.sh
#################@/
vi /sbin/auto_wifi.sh //然后编辑自启动启动脚本内容
/@#################
#!/bin/sh
if [ $ACTION = "add" ];
then
wpa_supplicant -B -c/etc/wpaconf/aes_tkip.conf -ira0
wpa_cli -a/sbin/wpa_action.sh -B
else
killall wpa_supplicant
killall wpa_cli
killall dhclient
fi
#################@/
chmod +x /sbin/auto_wifi.sh //加上可执行权限
7.4、实现自动执行dhclient
打开wpa_supplican的README文件查看操作方法
/@#################
Using wpa_cli to run external program on connect/disconnect
-----------------------------------------------------------
wpa_cli can used to run external programs whenever wpa_supplicant
connects or disconnects from a network. This can be used, e.g., to
update network configuration and/or trigget DHCP client to update IP
addresses, etc.
One wpa_cli process in "action" mode needs to be started for each
interface. For example, the following command starts wpa_cli for the
default ingterface (-i can be used to select the interface in case of
more than one interface being used at the same time):
做法:运行这个脚本,把要执行的命令写在这个脚本里
wpa_cli -a/sbin/wpa_action.sh -B
The action file (-a option, /sbin/wpa_action.sh in this example) will
be executed whenever wpa_supplicant completes authentication (connect
event) or detects disconnection). The action script will be called
with two command line arguments: interface name and event (CONNECTED
or DISCONNECTED). If the action script needs to get more information
about the current network, it can use 'wpa_cli status' to query
wpa_supplicant for more information.
Following example can be used as a simple template for an action
script:
#!/bin/sh
IFNAME=$1 #哪一个网卡
CMD=$2 #CONNECTED/DISCONNECTED这两个命令
if [ "$CMD" = "CONNECTED" ]; then
SSID=`wpa_cli -i$IFNAME status | grep ^ssid= | cut -f2- -d=`
# configure network, signal DHCP client, etc.
fi
if [ "$CMD" = "DISCONNECTED" ]; then
# remove network configuration, if needed
SSID=
fi
#################@/
vi /sbin/wpa_action.sh //首先创建一个脚本,让其自动运行/或退出dhclient
/@#################
#!/bin/sh
IFNAME=$1
CMD=$2
if [ "$CMD" = "CONNECTED" ]; then
echo connect $IFNAME, dhclient for it > /dev/console
dhclient $IFNAME
fi
if [ "$CMD" = "DISCONNECTED" ]; then
echo disconnect $IFNAME, kill dhclient for it > /dev/console
killall dhclient
fi
#################@/
做完这些后重启系统,就可以了。功能实现。
Water Droplet
598323431@qq.com
2020.4.7