Open main menu

OpenVZ Virtuozzo Containers Wiki β

User:Kir/cidr2wiki.sh

< User:Kir
Revision as of 15:18, 29 June 2011 by Kir (talk | contribs) (created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
cidr2mask() {
	local i mask=""
	local full_octets=$(($1/8))
	local partial_octet=$(($1%8))

	for i in 0 1 2 3; do
		if [ $i -lt $full_octets ]; then
			mask="${mask}255"
		elif [ $i -eq $full_octets ]; then
			mask="${mask}$((256 - (1 << (8 - $partial_octet))))"
		else
			mask="${mask}0"
		fi
		test $i -lt 3 && mask="${mask}."
	done

	echo $mask
}