[Gllug] "ip" command

Russell Howe rhowe at siksai.co.uk
Fri Feb 3 16:12:21 UTC 2006


On Fri, Feb 03, 2006 at 01:45:01PM +0000, Bruce Richardson wrote:
> On Fri, Feb 03, 2006 at 01:17:56PM +0000, Richard wrote:
> > 
> > Anyone know what the "ip" command is?
> 
> It's a networking swiss army knife.  Does the job of ifconfig and route
> (much more flexibily than either) and a range of other things like
> policy routing.  Indispensible tool for me.

Although I haven't found a way to get it to understand a dotted quad
netmask, only CIDR notation, which led me to produce this little gem of
shell:

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"
	)
}

-- 
Russell Howe       | Why be just another cog in the machine,
rhowe at siksai.co.uk | when you can be the spanner in the works?
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list