evenet/lib/ip.bash

40 lines
1.0 KiB
Bash
Raw Normal View History

format_ip6_from_mac() {
2015-02-19 08:36:40 +00:00
local template=$1
IFS=':'; set $2; unset IFS
printf "$template" 0x${3} 0x${4} 0x${5} 0x${6}
}
add_ip6() {
2015-02-19 08:36:40 +00:00
local interface=$1
local ip=$2
local prefixlen=$3
2015-02-21 08:41:13 +00:00
if has ip; then
_add_ip_linux "$ip" "$prefixlen" "$interface"
elif has ifconfig; then
ifconfig "$interface" inet6 "$ip" prefixlen "$prefixlen" delete || true
ifconfig "$interface" inet6 "$ip" prefixlen "$prefixlen" add
2015-02-19 08:36:40 +00:00
else
die "no suitable program found to configure the network interface, need iproute2 or ifconfig"
fi
}
add_ip4() {
local interface=$1
local ip=$2
local netmask=$3
2015-02-21 08:41:13 +00:00
if has ip; then
2015-02-19 08:36:40 +00:00
local cidr="$(mask2cidr $netmask)"
2015-02-21 08:41:13 +00:00
_add_ip_linux "$ip" "$cidr" "$interface"
elif has ifconfig; then
ifconfig "$interface" "$ip" netmask "$netmask" delete || true
ifconfig "$interface" "$ip" netmask "$netmask" add
2015-02-19 08:36:40 +00:00
else
die "no suitable program found to set ip address, need iproute2 or ifconfig"
fi
}
2015-02-21 08:41:13 +00:00
_add_ip_linux() {
ip addr del "$1/$2" dev "$3" || true
ip addr add "$1/$2" dev "$3"
}