Last active
August 20, 2023 14:28
-
-
Save ihipop/48da20c77d3bd4f0bcd7c75bc0f116d2 to your computer and use it in GitHub Desktop.
NetworkManager VPN Connection Guarder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# put me in the directory of "/etc/NetworkManager/dispatcher.d/",permission `u+x,g-w,o-w` | |
GUARDED_VPN_CONNECTION_NAMES='SSTP' | |
if [ -f "$CONTROL_FILE" ] || [ -f "$CONTROL_FILE-$CONNECTION_ID" ]; then | |
echo -n "VPN GUARD DISABLED" | |
[ -f "$CONTROL_FILE-$CONNECTION_ID" ] && echo -n " FOR CONNECTION ${CONNECTION_ID}" | |
echo "" | |
exit 0 | |
fi | |
[[ "vpn-down vpn-up up down connectivity-change" == *"${NM_DISPATCHER_ACTION}"* ]] || exit 0 | |
for VPN_CONNECTION_NAME in ${GUARDED_VPN_CONNECTION_NAMES}; do | |
if [ "${NM_DISPATCHER_ACTION}" = "vpn-up" ] && [ "${CONNECTION_ID}" = "${VPN_CONNECTION_NAME}" ]; then ## no action on self up and down | |
exit 0 | |
fi | |
( | |
sleep 2 | |
## nmcli c d "${VPN_CONNECTION_NAME}" ## if unplug the connection, the previous connection may still keep active in a shot time | |
for __ in {1..20}; do # try at most 20 times,about 40 seconds | |
nmcli c u "${VPN_CONNECTION_NAME}" && break | |
[ "$(nmcli c s --active "${VPN_CONNECTION_NAME}")" != "" ] && break | |
sleep 2 | |
done | |
) & | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment