For a certain option I need to calculate the Network address from an IP address and a given netmask - on a router with Routertech Busybox bash (for a "route -net" command)
There are some examples on the web - but only for /24 to /32 and they use "awk" which we don't have, also plenty of javascript type web pages which didn't prove much use...
I think this does script extract does it, but do not know how to exhaustively test - and I'm sure there must be an easier way...
Code: Select all
#!/bin/sh
if [ "$2" ]; then
NM="$2"
else
NM="248.0.0.0"
fi
if [ "$1" ]; then
IP="$1"
else
IP="255.253.252.100"
fi
#
n="${NM%.*}";m="${NM##*.}"
l="${IP%.*}";r="${IP##*.}";c=""
if [ "$m" = "0" ]; then
c=".0"
m="${n##*.}";n="${n%.*}"
r="${l##*.}";l="${l%.*}"
if [ "$m" = "0" ]; then
c=".0$c"
m="${n##*.}";n="${n%.*}"
r="${l##*.}";l="${l%.*}"
if [ "$m" = "0" ]; then
c=".0$c"
m=$n
r=$l;l=""
fi
fi
fi
let s=256-$m
let r=$r/$s*$s
if [ "$l" ]; then
SNW="$l.$r$c"
else
SNW="$r$c"
fi
#
echo $SNW
with IP=255.253.252.100, NM=248.0.0.0
ans = 248.0.0.0
For others say
Code: Select all
/var # ./subnet 192.168.100.1 255.255.255.0
192.168.100.0
Constructive criticism anyone?
Edit: Not a big problem, but if I get it wrong you get that wonderful error message
Code: Select all
MASQUERADE: No route: Rusty's brain broke!