-
-
Save aachyee/4fb090c9a5330b9e608fac72ef6c225f to your computer and use it in GitHub Desktop.
Convert netmask to cidr.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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