Skip to content

Instantly share code, notes, and snippets.

@islander
Last active August 20, 2019 06:08
Show Gist options
  • Save islander/bf73d632ed85f009e98ca76d53f5e0c5 to your computer and use it in GitHub Desktop.
Save islander/bf73d632ed85f009e98ca76d53f5e0c5 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Info:
# Add static route on interface up.
# Suitable for CentOS 7.
# $DHCP4_ROUTERS is dispatcher environment variable.
# Others can be found by switching DEBUG mode.
#
# Install:
# ~# cp 99-static.sh /etc/NetworkManager/dispatcher.d/
# ~# chmod +x /etc/NetworkManager/dispatcher.d/99-static.sh
#
set -euo pipefail
PATH=/bin:/usr/bin:/sbin
LOG='/var/log/nm_dispatcher.log'
INTERFACE=$1
STATUS=$2
DST_HOST="8.8.8.8"
GW="${DHCP4_ROUTERS%% *}" # get first gateway
DEBUG="false"
if [[ "$DEBUG" == "true" ]]; then
set -x # debug mode
echo "================" > /tmp/variables.txt
echo "Environment Variables" >> /tmp/variables.txt
printenv >> /tmp/variables.txt
echo "===============" >> /tmp/variables.txt
echo "Shell Variables" >> /tmp/variables.txt
set >> /tmp/variables.txt
echo "[$(date +"%F %T")] Called with ($*)" >> ${LOG}
fi
[[ "$INTERFACE" == "eth0" ]] || exit
if [[ "$STATUS" == "up" ]]; then
ip route add $DST_HOST via $GW dev "$INTERFACE"
fi
if [[ "$STATUS" == "down" ]]; then
ip route del $DST_HOST
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment