![如何从 Gentoo 的 OpenRC /etc/conf.d/net 中的 postup() 访问 SSID 信息](https://linux22.com/image/1316334/%E5%A6%82%E4%BD%95%E4%BB%8E%20Gentoo%20%E7%9A%84%20OpenRC%20%2Fetc%2Fconf.d%2Fnet%20%E4%B8%AD%E7%9A%84%20postup()%20%E8%AE%BF%E9%97%AE%20SSID%20%E4%BF%A1%E6%81%AF.png)
我想根据我所连接的无线网络启动 openvpn(或停止它)。
但env
倾销张贴()并不表示${SSID}
当时可用。
盯着净/wpa_supplicant.sh似乎没有帮助,所以我向 hivemind 寻求帮助。
答案1
我原本想不根据我连接的 SSID 启动 openvpn。但我想到的更好的解决方案是看看 IP。
/etc/conf.d/net:
postup() {
# https://stackoverflow.com/questions/845694/how-do-i-find-my-computers-ip-address-using-the-bash-shell
IP=$(ifconfig ${IFACE} | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p')
OPENVPN=$(if [[ "${IFACE}" != "tun0" && ${IP%.*} != 192.168.[13] ]]; then echo "YES"; fi)
sw-notify-send -i "${ICONS}/network-status.png" "${IFACE}" "$(ip addr show dev ${IFACE} | grep inet)"
if [[ -n ${OPENVPN} ]]; then
sw-notify-send -i "${ICONS}/network-status.png" "${IFACE}" "No IP range conflict detected for ${IP}, starting openvpn"
/etc/init.d/openvpn -D restart
fi
return 0
}
编辑更进一步的是,我现在把这个逻辑转移到了DHCP服务器钩子。要使其工作,您应该从您的特殊 DHCP 服务器发送一些唯一的 dhcp 选项值:我在这里使用选项 114“default_url”。
/etc/dhcpcd.enter-hook:
ACTION="stop"
COMMAND="openvpn.noroutes"
if [ "$reason" = "BOUND" ]; then
if [ ! -z ${new_default_url} ]; then
sw-notify-send $ifname "Detected DHCP default_url $new_default_url, skipping openvpn"
exit 1
fi
OPENVPN=$(if [[ $ifname != "tun0" && ${new_network_number%.*} != 192.168.[13] ]]; then echo "YES"; fi)
if [[ -n $OPENVPN ]]; then
sw-notify-send $ifname "IP range for $new_ip_address is free, starting openvpn"
COMMAND="openvpn"
fi
ACTION="restart"
/etc/init.d/"${COMMAND}" -D "${ACTION}"
fi