我想根据我所连接的无线网络启动 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