我有一台通过 eth0 连接到互联网的电脑,并且有一个 USB wifi 加密狗:我成功将其配置为热点,我可以从我的移动设备连接到该热点,以便我可以共享电脑互联网连接。
为此,我编写了两个脚本:ap-on
激活 AP 和ap-off
停用它,它们工作正常。 (我会将它们放在消息末尾,以供参考)。
我想要的是当我插入 USB 密钥时它们会自动触发。所以我写了一个 udev 规则,看起来像
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:13:46:78:c5:4e", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan2", RUN+="/usr/local/bin/ap-on > /dev/null 2>&1"
发生的情况是ap-on
在热插拔时启动,但设备 wlan2 未正确配置:ifconfig
我得到
wlan2 Link encap:Ethernet IndirizzoHW 00:13:46:78:c5:4e
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisioni:0 txqueuelen:1000
Byte RX:0 (0.0 B) Byte TX:0 (0.0 B)
而当我手动启动它时我得到
mon.wlan2 Link encap:UNSPEC IndirizzoHW 00-13-46-78-C5-4E-3A-30-00-00-00-00-00-00-00-00
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisioni:0 txqueuelen:1000
Byte RX:0 (0.0 B) Byte TX:0 (0.0 B)
wlan2 Link encap:Ethernet IndirizzoHW 00:13:46:78:c5:4e
indirizzo inet:192.168.23.1 Bcast:192.168.23.255 Maschera:255.255.255.0
indirizzo inet6: fe80::213:46ff:fe78:c54e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:43 errors:0 dropped:0 overruns:0 carrier:0
collisioni:0 txqueuelen:1000
Byte RX:0 (0.0 B) Byte TX:7751 (7.7 KB)
为什么会出现这种情况?
第二个问题:如何更改ap-off
删除 USB 密钥时启动的规则?
这是ap-on
:
#!/bin/bash
# Config files
hotspotconfig="/etc/hostapd/my-ap-hotspot.conf"
dhcpdconfig="/etc/dhcp/my-dhcpd-hotspot.conf"
# Network interface to the internet
NETnic=eth0
# Wireless network interface for the access point
APnic=wlan2
# Write the hostapd config file
cat <<EOF | tee "$hotspotconfig" > /dev/null 2>&1
interface=$APnic
driver=nl80211
ssid=hotspot-$APnic
hw_mode=g
channel=2
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=bhu87ygv
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF
# Write the dhcpd config file
cat <<EOF | tee "$dhcpdconfig" > /dev/null 2>&1
default-lease-time 86400;
max-lease-time 864000;
subnet 192.168.23.0 netmask 255.255.255.0 {
range 192.168.23.20 192.168.23.25;
option domain-name-servers 8.8.8.8;
option routers 192.168.23.1;
option broadcast-address 192.168.23.255;
}
EOF
#set IP address
ifconfig $APnic 192.168.23.1
## start hostapd
hostapd -d $hotspotconfig > /var/log/hostapd.log &
sleep 2
# launch dhcpd daemon
dhcpd -cf $dhcpdconfig $APnic
sleep 2
# set kernel variable(s)
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/conf/all/forwarding
echo "1" > /proc/sys/net/ipv6/conf/default/forwarding
# create iptables rules
iptables -t nat -A POSTROUTING -s 192.168.23.0/24 -o $NETnic -j MASQUERADE
exit 0
这是ap-off
#!/bin/bash
# Config files
hotspotconfig="/etc/hostapd/my-ap-hotspot.conf"
dhcpdconfig="/etc/dhcp/my-dhcpd-hotspot.conf"
# Network interface to the internet
NETnic=eth0
# Wireless network interface for the access point
APnic=wlan2
# stop hostapd and dhcpd
killall hostapd
killall dhcpd
# remove iptables rules
iptables -t nat -F
# set kernel variable(s)
echo "0" > /proc/sys/net/ipv4/ip_forward
echo "0" > /proc/sys/net/ipv4/conf/all/forwarding
echo "0" > /proc/sys/net/ipv6/conf/default/forwarding
# unconfig wifi nic
ifconfig $APnic down
# remove config files
rm -f $hotspotconfig
rm -f $dhcpdconfig
exit 0