From 34ba3b555aab37cce860000407c113bb8b4e3442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sat, 21 Feb 2015 09:41:13 +0100 Subject: [PATCH] always use iproute2 on linux --- lib/ip.bash | 31 ++++++++++++++++--------------- lib/mac.bash | 21 ++++++++++++++------- lib/route.bash | 32 +++++++++++--------------------- 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/lib/ip.bash b/lib/ip.bash index 9efd600..6abd43f 100644 --- a/lib/ip.bash +++ b/lib/ip.bash @@ -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" +} + diff --git a/lib/mac.bash b/lib/mac.bash index a4a668c..3b79ed3 100644 --- a/lib/mac.bash +++ b/lib/mac.bash @@ -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 diff --git a/lib/route.bash b/lib/route.bash index 0893585..dc7ad57 100644 --- a/lib/route.bash +++ b/lib/route.bash @@ -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