App-DHCPClientUtils
view release on metacpan or search on metacpan
scripts/multi-homed-routing.pl view on Meta::CPAN
# This script is auto-generated output of command:
# \\\$ [% cmd_line %]
# This script can be called as part of SysV initscripts.
# For example, in RedHat Linux, symlink this as file /sbin/ifup-local
# See Red Hat Enterprise Linux7 Networking Guide for details.
# This script can be called as a NetworkManager dispatcher.
# Symlink this script in directory /etc/NetworkManager/dispatcher.d/
# Agument handling:
IFUP_DEVICE=
if [ \\\$# -gt 0 ]; then
# Check if --argument or inteface name
if [[ \\\$1 != --* ]]; then
# Assume SysV init or Networkmanager interface name
IFUP_DEVICE=\\\$1
shift
# Networkmanager?
if [ \\\$# -gt 0 ]; then
ACTION=\\\$1
shift
if [ "\\\$ACTION" != "up" ]; then
# Run only on interface-up.
exit
fi
fi
fi
fi
# Id for this ruleset:
RULES_ID="[% rules_id %]"
RULES_DIR="/var/cache/routing-rules"
IP_TOOL=/sbin/ip
# Set up my interfaces
[% FOREACH if IN interfaces %]
IF[% if.table %]_DEVICE=[% if.device %]
IF[% if.table %]_IP=[% if.address %]
IF[% if.table %]_NET=[% if.network %]
[% IF if.gateway %]
IF[% if.table %]_GW=[% if.gateway %]
[% END %]
[% IF if.weight %]
IF[% if.table %]_WEIGHT=[% if.weight %]
[% END %]
[% END %]
# Helper functions
function verify_ip() {
local IP
local device=\\\$1
local IP_in=\\\$2
IP=\\\$(\\\$IP_TOOL -4 -o addr show dev \\\$device 2> /dev/null)
if [[ \\\$IP =~ inet[[:space:]]+([0-9.]+)/ ]]; then
if [ \\\${BASH_REMATCH\[1\]} != \\\$IP_in ]; then
echo "IP-address for \\\$device expected to be \\\$IP_in, got \\\${BASH_REMATCH\[1\]}."
echo "Re-run multi-homed-routing.pl to update routing setup."
return 1
fi
else
echo "Failed to parse IP-address for interface \\\$device."
echo "Probably interface is not up. Routing setup called too early?"
return 5
fi
return 0
}
# Keep track of interfaces. We need all of them for this to work properly.
fail_cnt=0
[% FOREACH if IN interfaces %]
# Check interface [% if.device %]
verify_ip \\\$IF[% if.table %]_DEVICE \\\$IF[% if.table %]_IP
if [ \\\$? -eq 0 ]; then
# Add table [% if.table %] for interface [% if.device %]
\\\$IP_TOOL route flush table [% if.table %]
[% FOREACH if2 IN interfaces %]
[% other_device = if2.device %][% IF if.device == if2.device %]
\\\$IP_TOOL route add \\\$IF[% if2.table %]_NET dev \\\$IF[% if2.table %]_DEVICE src \\\$IF[% if2.table %]_IP table [% if.table %]
[% ELSIF if.crossref.\$other_device == 1 %]
\\\$IP_TOOL route add \\\$IF[% if2.table %]_NET dev \\\$IF[% if2.table %]_DEVICE table [% if.table %]
[% ELSE %]
# Not adding. Shares same network [% if.network %]:
#\\\$IP_TOOL route add [% if2.network %] dev [% if2.device %] table [% if.table %]
[% END %]
[% END %]
[% FOREACH route IN if.add_routes %]
\\\$IP_TOOL route add [% route %] table [% if.table %]
[% END %]
\\\$IP_TOOL route add 169.254.0.0/16 dev \\\$IF[% if.table %]_DEVICE scope link metric [% if.apipa_metric %] table [% if.table %]
\\\$IP_TOOL route add 127.0.0.0/8 dev lo table [% if.table %]
[% IF if.gateway %]
\\\$IP_TOOL route add default via \\\$IF[% if.table %]_GW table [% if.table %]
[% END %]
else
fail_cnt=\\\$((fail_cnt+1))
fi
[% END %]
# Proceed routing setup only if all interfaces are ok.
if [ \\\$fail_cnt -gt 0 ]; then
exit 1
fi
# Save existing, if running the first time:
if [ -e "\\\${RULES_DIR}" ] && [ ! -e "\\\${RULES_DIR}/\\\${RULES_ID}.original" ]; then
SAVE_FILENAME="\\\${RULES_DIR}/\\\$RULES_ID.original"
echo "# Rules saved at: \\\$(date +"%F %H:%M:%S")" > "\\\$SAVE_FILENAME"
echo "# ip rule list:" >> "\\\$SAVE_FILENAME"
echo "\\\$(\\\$IP_TOOL rule list)" >> "\\\$SAVE_FILENAME"
echo "# ip route show all (table main)" >> "\\\$SAVE_FILENAME"
echo "\\\$(\\\$IP_TOOL route show all)" >> "\\\$SAVE_FILENAME"
[% FOREACH if IN interfaces %]
# echo "# ip route show all table [% if.table %]" >> "\\\$SAVE_FILENAME"
# echo "\\\$(\\\$IP_TOOL route show all table [% if.table %])" >> "\\\$SAVE_FILENAME"
[% END %]
fi
# All good. Set up my main routing table
\\\$IP_TOOL route flush all table main
[% FOREACH if IN interfaces %]
[% IF if.in_main_table %]
( run in 2.030 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )