ff_fix_default_route 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/perl -W
  2. #
  3. # Maximilian Wilhelm <max@rfc2324.org>
  4. # -- Sat 11 Feb 2017 10:29:29 PM CET
  5. #
  6. use strict;
  7. # Search for interface entry for an external interface with an IPv4 default
  8. # route configured, like this:
  9. #
  10. #auto eth0
  11. #iface eth0
  12. # address 5.196.106.54/32
  13. # gateway 5.196.106.48
  14. # mtu 1500
  15. # pointopoint 5.196.106.48
  16. my $gateway = undef;
  17. open (ENI, "< /etc/network/interfaces")
  18. or die "Failed to open '/etc/network/interfaces': $!\n";
  19. while (my $line = <ENI>) {
  20. chomp $line;
  21. # New interface stanza
  22. if ($line =~ /^iface (.*)/) {
  23. $gateway = undef;
  24. }
  25. # gateway set?
  26. elsif ($line =~ m/gateway\s+([0-9.]+)$/) {
  27. $gateway = $1;
  28. }
  29. # Interface part of vrf_external
  30. elsif ($line =~ m/vrf vrf_external/) {
  31. last;
  32. }
  33. }
  34. close (ENI);
  35. # If there's no gateway configured for vrf_external, nothing to do
  36. if (not defined $gateway) {
  37. exit (0);
  38. }
  39. # Check for current default route in vrf_external
  40. my $default_route_active = undef;
  41. open (ROUTE, "ip -4 route show table 1023 | grep ^default |")
  42. or die "Failed to read default route from table 1023: $!\n";
  43. while (my $line = <ROUTE>) {
  44. if ($line =~ m/^default via ([0-9.]+)/) {
  45. $default_route_active = $1;
  46. }
  47. }
  48. close (ROUTE);
  49. # If we didn't find an active default route, re-add it.
  50. if (not defined $default_route_active) {
  51. system ("ip route add default via $gateway table 1023");
  52. }