Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. gateway_ip=$(ip route show default | awk '/default/ {print $3}')
  6. eth1_ip=$(ip addr show eth1 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
  7.  
  8. echo "Configuring secondary IP (eth1) ..."
  9.  
  10. # Print stuff
  11. echo "Gateway IP: ${gateway_ip}"
  12. echo "Secondary IP: ${eth1_ip}"
  13.  
  14. # Enable DHCP autoconfiguration
  15. if ! grep -q "auto eth1" /etc/network/interfaces; then
  16. sed -i '/auto eth0/a auto eth1' /etc/network/interfaces
  17. echo ".. dhcp autoconfigure enabled"
  18. else
  19. echo ".. dhcp autoconfigure already enabled, skipping"
  20. fi
  21.  
  22. # Restart networking service
  23. echo ".. restarting network service"
  24. service networking restart
  25.  
  26. # Add an entry to the route table
  27. if ! grep -q "eth1_rt" /etc/iproute2/rt_tables; then
  28. echo "2 eth1_rt" >> /etc/iproute2/rt_tables
  29. echo ".. route table entry added"
  30. else
  31. echo ".. route table entry exists, skipping"
  32. fi
  33.  
  34. # Add the default route for eth1
  35. if ! ip route list table eth1_rt 2>/dev/null | grep -q ${gateway_ip}; then
  36. ip route add default via ${gateway_ip} dev eth1 table eth1_rt
  37. echo ".. default route added"
  38. else
  39. echo ".. default route exists, skipping"
  40. fi
  41.  
  42. # Add rule to route traffic with a source of "eth1_ip" to the eth1 route table
  43. if ! ip rule show | grep -q ${eth1_ip}; then
  44. ip rule add from ${eth1_ip} lookup eth1_rt prio 1000
  45. echo ".. ip rule added"
  46. else
  47. echo ".. ip rule exists, skipping"
  48. fi
  49.  
  50. echo "Configuration complete!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement