Skip to content

Instantly share code, notes, and snippets.

@devkid devkid/vpn-domains.sh
Last active Oct 13, 2017

Embed
What would you like to do?
NetworkManager dispatcher script to add certain domains for a VPN device when using systemd-resolved (place in /etc/NetworkManager/dispatcher.d, make it executable and adapt VPN domains and connection name)
#!/bin/bash
set -ex
# ----------------------------------------------------------------------------
# Configuration:
case "$CONNECTION_ID"
in
"Some VPN name") # enter name of VPN connection in NetworkManager here
DOMAINS=( # enter domains that should be routed over the VPN here
some-domain.com
another-domain.net
)
;;
# repeat for more VPN connections
*)
exit 0
;;
esac
# ----------------------------------------------------------------------------
DBUS_PATH="/org/freedesktop/resolve1"
DBUS_IF="org.freedesktop.resolve1"
INTERFACE="$1"
IF_INDEX="$(cat "/sys/class/net/$INTERFACE/ifindex")"
if [[ $2 != "vpn-up" ]]
then
exit 0
fi
DOMAIN_STR=""
FIRST=true
for DOMAIN in ${DOMAINS[@]}
do
if [[ $FIRST == true ]]
then
FIRST=false
else
DOMAIN_STR="${DOMAIN_STR}, "
fi
DOMAIN_STR="${DOMAIN_STR}('${DOMAIN}', true)"
done
gdbus call --system --dest $DBUS_IF --object-path $DBUS_PATH --method $DBUS_IF.Manager.SetLinkDomains $IF_INDEX "[$DOMAIN_STR]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.