Last active
June 21, 2024 01:42
-
-
Save ihipop/48da20c77d3bd4f0bcd7c75bc0f116d2 to your computer and use it in GitHub Desktop.
NetworkManager Connection Guarder
This file contains hidden or 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_CONNECTION_NAMES='SSTP' | |
CONTROL_FILE=/tmp/NM-CONN-GUARD-DISABLED | |
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 GUARDED_CONNECTION_NAME in ${GUARDED_CONNECTION_NAMES}; do | |
if [[ "${NM_DISPATCHER_ACTION}" == *"up" ]] && [ "${CONNECTION_ID}" = "${GUARDED_CONNECTION_NAME}" ]; then ## no action on self up and down | |
exit 0 | |
fi | |
( | |
sleep 2 | |
## nmcli c d "${GUARDED_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 s --active "${GUARDED_CONNECTION_NAME}")" != "" ] && break | |
nmcli c u "${GUARDED_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