Skip to content

Instantly share code, notes, and snippets.

@alfredkrohmer
Last active October 13, 2017 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredkrohmer/266b62365c772a9cd9d7ff1aca22f72b to your computer and use it in GitHub Desktop.
Save alfredkrohmer/266b62365c772a9cd9d7ff1aca22f72b to your computer and use it in GitHub Desktop.
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