evenet/lib/route.bash

58 lines
1.7 KiB
Bash
Raw Normal View History

2015-02-19 08:36:40 +00:00
add_route4() {
local interface="$1"
local subnet="$2"
local gateway="$3"
local netmask="$4"
local cidr="$(mask2cidr $netmask)"
if has route; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
2015-02-19 16:55:45 +00:00
route -n del -net "$subnet" "$gateway" netmask "$netmask" "$interface" || true
route -n add -net "$subnet" "$gateway" netmask "$netmask" "$interface"
2015-02-19 08:36:40 +00:00
else
route -n del -net "$subnet/$prefixlen" "$gateway" || true
route -n add -net "$subnet/$prefixlen" "$gateway"
fi
elif has ip; then
ip route del "$subnet/$cidr" via "$gateway" dev "$interface" || true
ip route add "$subnet/$cidr" via "$gateway" dev "$interface"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
add_route6() {
local interface="$1"
local subnet="$2"
local gateway="$3"
local prefixlen="$4"
if has route; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
2015-02-19 16:55:45 +00:00
route -n del -A inet6 "$subnet/$prefixlen" gw "$gateway" "$interface" || true
route -n add -A inet6 "$subnet/$prefixlen" gw "$gateway" "$interface"
2015-02-19 08:36:40 +00:00
else
if [[ "$gateway" == fe80* ]]; then
gateway="$gateway%$interface"
fi
route -n add -inet6 "$network" "$gateway" -prefixlen "$prefixlen" || true
route -n add -inet6 "$network" "$gateway" -prefixlen "$prefixlen"
fi
elif has ip; then
ip route del "$subnet/$prefixlen" via "$gateway" dev "$interface" || true
ip route add "$subnet/$prefixlen" via "$gateway" dev "$interface"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
}
set_route_policy(){
# TODO: other operating systems
if [[ "$OSTYPE" != "linux-gnu" ]]; then
return
fi
if !has ip; then
warn "Could not set source "
fi
}