network etc host swapper
Note
a useful script to switch my homelabs external domains to internal IPs when coming / leave home
Files
#!/usr/bin/bash
# ------------------------------------------
#
# place in /etc/NetworkManager/dispatcher.d/
#
# ------------------------------------------
CURRENT_INTERFACE=$1
EVENT=$2
# uuid of 'tofu' (nmcli c)
HOME_UUID="xyz"
# Replace with the interface name (e.g., wlp0s20f3)
HOME_INTERFACE="wlp0s20f3"
# Path to your default hosts file
BASE_HOSTS="/etc/hosts.base"
# Path to the hosts file for your home network
HOME_HOSTS="/etc/hosts.home"
if [[ $CURRENT_INTERFACE != "$HOME_INTERFACE" ]] || [[ $EVENT != "up" && $EVENT != "down" ]]
; then
exit 0
fi
# this environmental variable is set on UP/DOWN events
if [[ $CONNECTION_UUID != "$HOME_UUID" ]]; then
exit 0
fi
# Function to swap hosts files
if [ "$EVENT" = "up" ]; then
cat "$BASE_HOSTS" "$HOME_HOSTS" | sudo tee /etc/hosts
echo "combined home hosts file"
else
sudo cp /etc/hosts.base /etc/hosts
echo "back to base hosts file"
fi