Skip to content

Instantly share code, notes, and snippets.

@aachyee
Forked from tiveone/netutils.sh
Created September 24, 2020 10:19
Show Gist options
  • Select an option

  • Save aachyee/4fb090c9a5330b9e608fac72ef6c225f to your computer and use it in GitHub Desktop.

Select an option

Save aachyee/4fb090c9a5330b9e608fac72ef6c225f to your computer and use it in GitHub Desktop.
Convert netmask to cidr.
#!/bin/bash
function ::netmask2cidr()
{
case $1 in
0x*)
local hex=${1#0x*} quad=
while [ -n "${hex}" ]; do
local lastbut2=${hex#??*}
quad=${quad}${quad:+.}0x${hex%${lastbut2}*}
hex=${lastbut2}
done
set -- ${quad}
;;
esac
local i= len=
local IFS=.
for i in $1; do
while [ ${i} != "0" ]; do
len=$((${len} + ${i} % 2))
i=$((${i} >> 1))
done
done
echo "${len}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment