- improve netname, add: netcheck, list_static_iface, read_iface_addr, write_iface_addr
This commit is contained in:
Stanislav Ievlev
2008-10-21 15:25:17 +04:00
parent 77c9ce3f37
commit c94e3898e9
4 changed files with 89 additions and 34 deletions

View File

@@ -1,8 +1,8 @@
%define _altdata_dir %_datadir/alterator
Name: alterator-net-common
Version: 0.4
Release: alt4
Version: 0.5
Release: alt1
Packager: Stanislav Ievlev <inger@altlinux.org>
@@ -31,15 +31,8 @@ helpers for etcnet administration
%_libexecdir/%name/
%changelog
* Thu Oct 02 2008 Vladislav Zavjalov <slazav@altlinux.org> 0.4-alt4
- cleanup sh-code
* Wed Oct 01 2008 Vladislav Zavjalov <slazav@altlinux.org> 0.4-alt3
- read_iface_option(): work with unexistent iface dirs
* Wed Oct 01 2008 Vladislav Zavjalov <slazav@altlinux.org> 0.4-alt2
- add support for default/options and default/options-<type>
to alterator-net-functions/read_iface_option()
* Tue Oct 21 2008 Stanislav Ievlev <inger@altlinux.org> 0.5-alt1
- improve netname, add: netcheck, list_static_iface, read_iface_addr, write_iface_addr
* Thu Aug 28 2008 Stanislav Ievlev <inger@altlinux.org> 0.4-alt1
- add functions: netname, list_iface, list_network

View File

@@ -10,20 +10,19 @@ write_iface_option()
shell_config_set "$1/options" "$2" "$3"
}
write_iface_addr()
{
echo "$2" >"$1/ipv4address"
}
read_iface_option()
{
# shell_config_get always returns 0,
# so we can't merge commands with || :(
shell_config_get "$1/options" "$2"
}
local ret=$(shell_config_get "$1/options" "$2")
if [ -z "$ret" ]; then
local type=$(shell_config_get "$1/options" "TYPE")
ret=$(shell_config_get "/etc/net/ifaces/default/options-$type" "$2")
fi
if [ -z "$ret" ]; then
ret=$(shell_config_get "/etc/net/ifaces/default/options" "$2")
fi
echo "$ret"
read_iface_addr()
{
[ ! -s "$1/ipv4address" ] || head -n1 "$1/ipv4address"
}
### string ppp options
@@ -108,6 +107,16 @@ list_iface()
grep -v 'lo'
}
list_static_iface()
{
for i in /etc/net/ifaces/*; do
local name="${i##*/}"
[ "$name" != "lo" -a "$name" != "default" -a "$name" != "unknown" ] || continue
[ "$(read_iface_option $i BOOTPROTO)" = "static" -a -s "$i/ipv4address" ] || continue
echo "${i##*/}"
done
}
list_network()
{
for i in /etc/net/ifaces/*; do
@@ -117,10 +126,11 @@ list_network()
local proto="$(read_iface_option $i BOOTPROTO)"
local addr=
if [ "$proto" = "static" ];then
addr="$(cat $i/ipv4address)"
addr="$(read_iface_addr $i)"
else
addr="$(/sbin/ip a s "$name" 2>/dev/null|sed -n 's,[[:space:]]\+inet[[:space:]]\+\([.0-9/]\+\).*,\1,p')"
fi
[ -z "$addr" ] || netname "$addr"
[ -z "$addr" ] || netname "$addr" | cut -f1
done
}

View File

@@ -0,0 +1,31 @@
#!/bin/ash -efu
net_ip="${1%%/*}"
net_len="${1##*/}"
ext_ip1="$2"
ext_ip2="$3"
position=$((0xffffffff))
[ "$net_len" -ge 1 -a "$net_len" -le 32 ] 2>/dev/null
mask="$(( $position - ($position >> $net_len) ))"
wildcard="$(( ~mask & $position))"
OLD_IFS="$IFS"
IFS=.
set ${ext_ip1} ${ext_ip2}
IFS="$OLD_IFS"
#network
n1="$(($mask >> 24 & $1 & 0xff))"
n2="$(($mask >> 16 & $2 & 0xff))"
n3="$(($mask >> 8 & $3 & 0xff))"
n4="$(($mask & $4 & 0xff))"
n5="$(($mask >> 24 & $5 & 0xff))"
n6="$(($mask >> 16 & $6 & 0xff))"
n7="$(($mask >> 8 & $7 & 0xff))"
n8="$(($mask & $8 & 0xff))"
[ "$n1.$n2.$n3.$n4" = "$net_ip" ] &&
[ "$n5.$n6.$n7.$n8" = "$net_ip" ] &&
[ "$1" -le "$5" -a "$2" -le "$6" -a "$3" -le "$7" -a "$4" -le "$8" ]

View File

@@ -1,18 +1,39 @@
#!/bin/sh -e
#!/bin/sh -efu
ip="${1%%/*}"
len="${1##*/}"
position=$((0xFFFFFFFF))
[ "$len" -ge 0 -a "$len" -le 32 ] 2>/dev/null
mask=$(( $position - ($position >> $len) ))
position=$((0xffffffff))
[ "$len" -ge 1 -a "$len" -le 32 ] 2>/dev/null
mask="$(( $position - ($position >> $len) ))"
wildcard="$(( ~mask & $position))"
IFS="."
set $ip
printf '%s.%s.%s.%s/%s\n' \
"$(($mask >> 24 & $1))" \
"$(($mask >> 16 & $2))" \
"$(($mask >> 8 & $3))" \
"$(($mask & $4))" \
"$len"
#network
n1="$(($mask >> 24 & $1 & 0xff))"
n2="$(($mask >> 16 & $2 & 0xff))"
n3="$(($mask >> 8 & $3 & 0xff))"
n4="$(($mask & $4 & 0xff))"
#broadcast
b1="$(($wildcard >> 24 & 0xff | $1))"
b2="$(($wildcard >> 16 & 0xff | $2))"
b3="$(($wildcard >> 8 & 0xff | $3))"
b4="$(($wildcard & 0xff | $4))"
printf '%s.%s.%s.%s/%s' \
"$n1" "$n2" "$n3" "$n4" "$len"
if [ "$len" -lt 31 ] ;then
printf ' %s.%s.%s.%s %s.%s.%s.%s\n' \
"$n1" "$n2" "$n3" "$(( $n4 | 1 ))" \
"$b1" "$b2" "$b2" "$(( $b4 & 0xfe ))"
elif [ "$len" -eq 31 ] ;then
printf ' %s.%s.%s.%s %s.%s.%s.%s\n' \
"$n1" "$n2" "$n3" "$n4" \
"$b1" "$b2" "$b3" "$b4"
else
printf ' %s %s\n' "$ip" "$ip"
fi