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 sed -i "/^NETCONFIG_DNS_STATIC_SEARCHLIST=/ s/=.*$/=\"${all_search_domains}\"/" /etc/sysconfig/network/config
fi 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 if [ -n "${gateway}" ]; then
echo "defaultrouter=\"${gateway}\"" >> $routing_conf echo "defaultrouter=\"${gateway}\"" >> $routes_conf_path
fi fi
# Add static routes # Add static routes
if [ -n "${ROUTES}" ]; then 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" IFS=',' read -r -a routes <<< "$ROUTES"
route_names="" route_names=""
routes_info=() routes_conf=()
for index in "${!routes[@]}" for index in "${!routes[@]}"
do do
route_name="lan${index}" route_name="lan${index}"
route_names="${route_names}${route_name} " 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]} dst=${route_info[0]}
static_gw=${route_args[1]} gw=${route_info[1]}
out_iface=${route_args[2]} iface=${route_info[2]}
route_info="${route_name}=\"-net${subnet} ${static_gw}\"" route_conf="${route_name}=\"-net${dst} ${gw}\""
[ -n "${out_iface}" ] && route_info=" ${route_info} -iface ${dev}" [ -n "${iface}" ] && route_conf=" ${route_conf} -iface ${dev}"
routes_info+=(route_info) routes_conf+=(route_conf)
done 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 do
echo -e "${route_info}\n" >> $routing_conf echo -e "$route_conf\n" >> $routes_conf_path
done done
fi fi

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

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

@ -126,17 +126,20 @@ EOT
cat <<EOT cat <<EOT
[Route] [Route]
EOT EOT
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES" IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}" for route in "${routes[@]}"
do 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 if [[ "$route" == *"${dev}"* ]] || [[ "$dev" == "eth0" && "$route" != *"dev"* ]]; then
IFS=' via ' read -r -a route_args <<< "$route"
echo "Gateway=${route_args[1]}" IFS=' ' read -r -a route_info <<< "$(get_route_info "$route")"
echo "Destination=${route_args[0]}"
dst=${route_info[0]}
gw=${route_info[1]}
echo "Gateway=$gw"
echo "Destination=$dst"
echo "GatewayOnlink=yes" echo "GatewayOnlink=yes"
fi fi
done done

@ -122,15 +122,18 @@ gen_iface_conf()
# Add static routes # Add static routes
if [ -n "${ROUTES}" ]; then if [ -n "${ROUTES}" ]; then
# TODO: Validate possible comma with spaces
IFS=',' read -r -a routes <<< "$ROUTES" IFS=',' read -r -a routes <<< "$ROUTES"
for route in "${routes[@]}" for route in "${routes[@]}"
do 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 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 fi
done done

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

Loading…
Cancel
Save