static_routes
Daniel Clavijo Coca 2 years ago
parent d1f36a4202
commit 693844189a
No known key found for this signature in database
GPG Key ID: 073626228D14586A

@ -571,3 +571,30 @@ gen_resolvconf()
sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"${all_search_domains}\"/" /etc/sysconfig/network/config
fi
}
# Space separate source, destination and output interface of a route
get_route_info() {
route=$1
# Remove whitespaces beginning and end
route="${route# }"
route=$(echo "$route" | sed 's/[[:space:]]*$//')
# Parse traffic source, destination and outgoing interface
dst=$(echo "$route" | cut -d ' ' -f 1)
if [[ "$route" == *"via"* ]]; then
gw=${route##*" via "}
gw=${gw%" dev"*}
else
gw=''
fi
if [[ "$route" == *"dev"* ]]; then
iface=${route##*" dev "}
else
iface=''
fi
echo "$dst $gw $iface"
}

@ -117,43 +117,46 @@ gen_iface_conf()
###
routing_conf="/etc/rc.conf.d/routing"
routes_conf_path="/etc/rc.conf.d/routing"
if [ -n "${gateway}" ]; then
echo "defaultrouter=\"${gateway}\"" >> $routing_conf
echo "defaultrouter=\"${gateway}\"" >> $routes_conf_path
fi
# Add static routes
if [ -n "${ROUTES}" ]; then
# static_routes="lan mumoffice foo"
# route_lan="-net 192.168.1.0/24 192.168.1.254"
# route_mumoffice="-net 10.0.0.0/8 10.30.110.5"
# route_foo="-host 169.254.1.1 -iface lo0"
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
route_names=""
routes_info=()
routes_conf=()
for index in "${!routes[@]}"
do
route_name="lan${index}"
route_names="${route_names}${route_name} "
IFS=' via ' read -r -a route_args <<< "${routes[index]}"
IFS=' ' read -r -a route_info <<< "$(get_route_info "${routes[index]}")"
subnet=${route_args[0]}
static_gw=${route_args[1]}
out_iface=${route_args[2]}
dst=${route_info[0]}
gw=${route_info[1]}
iface=${route_info[2]}
route_info="${route_name}=\"-net${subnet} ${static_gw}\""
[ -n "${out_iface}" ] && route_info=" ${route_info} -iface ${dev}"
route_conf="${route_name}=\"-net${dst} ${gw}\""
[ -n "${iface}" ] && route_conf=" ${route_conf} -iface ${dev}"
routes_info+=(route_info)
routes_conf+=(route_conf)
done
echo -e "${route_names}\n" >> $routing_conf
echo -e "static_routes=\"${route_names}\"\n" >> $routes_conf_path
for route_info in "${routes_info[@]}"
for route_conf in "${routes_conf[@]}"
do
echo -e "${route_info}\n" >> $routing_conf
echo -e "$route_conf\n" >> $routes_conf_path
done
fi

@ -161,11 +161,11 @@ EOT
# Add static routes
if [ -n "${ROUTES}" ]; then
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}"
do
# Apply every route except the ones specified for other devices
if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then
echo " up ip route add $route"
fi

@ -150,16 +150,21 @@ EOT
# Add static routes
if [ -n "${ROUTES}" ]; then
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}"
do
# Apply every route except the ones specified for other devices
if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then
IFS=' via ' read -r -a route_args <<< "$route"
IFS=' ' read -r -a route_info <<< "$(get_route_info "$route")"
dst=${route_info[0]}
gw=${route_info[1]}
cat <<EOT
- to: "${route_args[0]}"
via: "${route_args[1]}"
- to: "$dst"
via: "$gw"
EOT
fi
done

@ -126,17 +126,20 @@ EOT
cat <<EOT
[Route]
EOT
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}"
do
# The route is targeted to the $dev iface
# Apply every route except the ones specified for other devices
if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then
IFS=' via ' read -r -a route_args <<< "$route"
echo "Gateway=${route_args[1]}"
echo "Destination=${route_args[0]}"
IFS=' ' read -r -a route_info <<< "$(get_route_info "$route")"
dst=${route_info[0]}
gw=${route_info[1]}
echo "Gateway=$gw"
echo "Destination=$dst"
echo "GatewayOnlink=yes"
fi
done

@ -122,15 +122,18 @@ gen_iface_conf()
# Add static routes
if [ -n "${ROUTES}" ]; then
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}"
do
IFS=' via ' read -r -a route_args <<< "$route"
IFS=' ' read -r -a route_info <<< "$(get_route_info "$route")"
dst=${route_info[0]}
gw=${route_info[1]}
# Apply every route except the ones specified for other devices
if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then
nmcli con mod "${dev}" ipv4.routes "${route_args[0]} ${route_args[1]}"
nmcli con mod "$dev" ipv4.routes "$dst" "$gw"
fi
done

@ -127,7 +127,6 @@ EOT
# Add static routes
if [ -n "${ROUTES}" ]; then
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}"

Loading…
Cancel
Save