[Gllug] "ip" command

Anthony Newman anthony.newman at uk.clara.net
Fri Feb 3 16:32:24 UTC 2006


Russell Howe wrote:
  > netmask_to_cidr() {
> 	echo "$1"| (
> 		IFS=.
> 		read NM1 NM2 NM3 NM4
> 
> 		NMDEC="$(((NM1 << 24) + (NM2 << 16) + (NM3 << 8) + NM4 ))"
> 
> 		if [ "$NMDEC" -gt $((0xffffffff)) -o "$NMDEC" -lt 0 ]; then
> 			echo "Invalid IPv4 netmask!" >&2
> 			exit 1
> 		fi
> 
> 		NM_BITS=0
> 		while [ "$NMDEC" -ne 0 ]; do
> 			NMDEC="$(((NMDEC << 1) & 0xffffffff))"
> 			NM_BITS="$((NM_BITS + 1))"
> 		done
> 
> 		return "$NM_BITS"
> 	)
> }
> 


I'll meet your ugly shell hack, and raise you:


echo "172.168.2.0/255.255.254.0" | perl -MNet::Netmask -pe 's/$_/new 
Net::Netmask($_)/e;'

172.168.2.0/23


Ant
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list