Skip to content

Instantly share code, notes, and snippets.

@zacblazic
Created July 26, 2017 23:35
Show Gist options
  • Save zacblazic/479aa9ce7210558a1308d1e93a9e0e80 to your computer and use it in GitHub Desktop.
Save zacblazic/479aa9ce7210558a1308d1e93a9e0e80 to your computer and use it in GitHub Desktop.
Configures a secondary network interface (Debian Jessie)
#!/bin/bash
set -e
gateway_ip=$(ip route show default | awk '/default/ {print $3}')
eth1_ip=$(ip addr show eth1 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
echo "Configuring secondary IP (eth1) ..."
# Print stuff
echo "Gateway IP: ${gateway_ip}"
echo "Secondary IP: ${eth1_ip}"
# Enable DHCP autoconfiguration
if ! grep -q "auto eth1" /etc/network/interfaces; then
sed -i '/auto eth0/a auto eth1' /etc/network/interfaces
echo ".. dhcp autoconfigure enabled"
else
echo ".. dhcp autoconfigure already enabled, skipping"
fi
# Restart networking service
echo ".. restarting network service"
service networking restart
# Add an entry to the route table
if ! grep -q "eth1_rt" /etc/iproute2/rt_tables; then
echo "2 eth1_rt" >> /etc/iproute2/rt_tables
echo ".. route table entry added"
else
echo ".. route table entry exists, skipping"
fi
# Add the default route for eth1
if ! ip route list table eth1_rt 2>/dev/null | grep -q ${gateway_ip}; then
ip route add default via ${gateway_ip} dev eth1 table eth1_rt
echo ".. default route added"
else
echo ".. default route exists, skipping"
fi
# Add rule to route traffic with a source of "eth1_ip" to the eth1 route table
if ! ip rule show | grep -q ${eth1_ip}; then
ip rule add from ${eth1_ip} lookup eth1_rt prio 1000
echo ".. ip rule added"
else
echo ".. ip rule exists, skipping"
fi
echo "Configuration complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment