Skip to content

Instantly share code, notes, and snippets.

@cseelye
Created August 17, 2017 17:27
Show Gist options
  • Save cseelye/09ea7f6288a89210b41402ea84a43161 to your computer and use it in GitHub Desktop.
Save cseelye/09ea7f6288a89210b41402ea84a43161 to your computer and use it in GitHub Desktop.
Helper script for Ubuntu network interface configuration
#!/bin/bash
set -o pipefail
pid=$BASHPID
log()
{
declare msg=${@:-$(</dev/stdin)}
logger --tag NETCONFIG "[${pid}] ${msg}"
}
runcommand()
{
log "Running \"$@\""
$@ 2>&1 | log || { log "Failed running \"$@\""; exit 1; }
}
[[ -z ${IFACE} ]] && exit 1
log "Running configure-interfaces for ${IFACE}"
# Set interface MTU
[[ -n ${IF_MTU} ]] && runcommand ip link set dev ${IFACE} mtu ${IF_MTU}
# Add symetric routing rules
[[ -n ${IF_ADDRESS} ]] && runcommand ip rule add from ${IF_ADDRESS} table ${IFACE}
[[ -n ${IF_ADDRESS} ]] && runcommand ip route add ${IF_NETWORK}/${IF_NETMASK} dev ${IFACE} scope link src ${IF_ADDRESS} table ${IFACE}
# Add the default route for this interface
[[ -n ${GATEWAY} ]] && runcommand ip route add default via ${GATEWAY} dev ${IFACE} src ${IF_ADDRESS} table ${IFACE}
# Add the system default route
[[ ${DEFAULT} == "1" ]] && runcommand ip route add default via ${GATEWAY}
# Add additional static routes
if [[ -n ${STATIC} && -n ${GATEWAY} ]]; then
for dest in ${STATIC}; do
runcommand ip route add ${dest} via ${GATEWAY} dev ${IFACE}
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment