always use iproute2 on linux

This commit is contained in:
Jörg Thalheim 2015-02-21 09:41:13 +01:00
parent c9047101bf
commit 34ba3b555a
3 changed files with 41 additions and 43 deletions

View File

@ -8,17 +8,11 @@ add_ip6(){
local interface=$1
local ip=$2
local prefixlen=$3
if has ifconfig; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
ifconfig "$interface" inet6 del "$ip/$prefixlen" || true
ifconfig "$interface" inet6 add "$ip/$prefixlen"
else
ifconfig "$interface" inet6 del "$ip" prefixlen "$prefixlen" || true
ifconfig "$interface" inet6 add "$ip" prefixlen "$prefixlen"
fi
elif has ip; then
ip addr del "$ip/$prefixlen" dev "$interface" || true
ip addr add "$ip/$prefixlen" dev "$interface"
if has ip; then
_add_ip_linux "$ip" "$prefixlen" "$interface"
elif has ifconfig; then
ifconfig "$interface" inet6 del "$ip" prefixlen "$prefixlen" || true
ifconfig "$interface" inet6 add "$ip" prefixlen "$prefixlen"
else
die "no suitable program found to configure the network interface, need iproute2 or ifconfig"
fi
@ -28,12 +22,19 @@ add_ip4() {
local interface=$1
local ip=$2
local netmask=$3
if has ifconfig; then
ifconfig "$interface" "$ip" netmask "$netmask"
elif has ip; then
if has ip; then
local cidr="$(mask2cidr $netmask)"
ip addr add "$ip/$cidr" dev "$interface"
_add_ip_linux "$ip" "$cidr" "$interface"
elif has ifconfig; then
ifconfig "$interface" del "$ip" netmask "$netmask" || true
ifconfig "$interface" add "$ip" netmask "$netmask"
else
die "no suitable program found to set ip address, need iproute2 or ifconfig"
fi
}
_add_ip_linux() {
ip addr del "$1/$2" dev "$3" || true
ip addr add "$1/$2" dev "$3"
}

View File

@ -1,23 +1,30 @@
set_mac() {
local interface=$1
local mac_address=$2
if has ifconfig; then
if has ip; then
current_mac=$(ip addr show dev evenet | awk '/link\/ether/ { print $2 }')
if [[ "${current_mac,,}" == "${mac_address,,}" ]]; then
return
fi
ip link set dev "$interface" down
ip link set dev "$interface" address "$mac_address"
ip link set dev "$interface" up
elif has ifconfig; then
current_mac=$(ifconfig "$interface" | awk '/ether/ {print $2}')
if [[ "${current_mac,,}" == "${mac_address,,}" ]]; then
return
fi
ifconfig "$interface" down
if [[ "$OSTYPE" == "linux-gnu" ]]; then
ifconfig "$interface" hw ether "$mac_address"
if [[ "$OSTYPE" == "freebsd"* ]]; then
ifconfig "$interface" link "$mac_address"
elif [[ "$OSTYPE" == "darwin"* ]]; then
ifconfig "$interface" lladdr "$mac_address"
else
ifconfig "$interface" ether "$mac_address"
fi
ifconfig "$interface" up
elif has ip; then
ip link set dev "$interface" down
ip link set dev "$interface" address "$mac_address"
ip link set dev "$interface" up
else
die "no suitable program found to configure the network interface, need iproute2 or ifconfig"
fi

View File

@ -5,17 +5,12 @@ add_route4() {
local netmask="$4"
local cidr="$(mask2cidr $netmask)"
if has route; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
route -n del -net "$subnet" "$gateway" netmask "$netmask" "$interface" || true
route -n add -net "$subnet" "$gateway" netmask "$netmask" "$interface"
else
route -n del -net "$subnet/$prefixlen" "$gateway" || true
route -n add -net "$subnet/$prefixlen" "$gateway"
fi
elif has ip; then
if has ip; then
ip route del "$subnet/$cidr" via "$gateway" dev "$interface" || true
ip route add "$subnet/$cidr" via "$gateway" dev "$interface"
elif has route; then
route -n del -net "$subnet/$prefixlen" "$gateway" || true
route -n add -net "$subnet/$prefixlen" "$gateway"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi
@ -27,20 +22,15 @@ add_route6() {
local gateway="$3"
local prefixlen="$4"
if has route; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then
route -n del -A inet6 "$subnet/$prefixlen" gw "$gateway" "$interface" || true
route -n add -A inet6 "$subnet/$prefixlen" gw "$gateway" "$interface"
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
if has ip; then
ip route del "$subnet/$prefixlen" via "$gateway" dev "$interface" || true
ip route add "$subnet/$prefixlen" via "$gateway" dev "$interface"
if has route; then
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"
else
die "no suitable program found to set routes, need iproute2 or ifconfig"
fi