Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Last active September 15, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanpuyue/7357484426658beeff2aa0919c2c831b to your computer and use it in GitHub Desktop.
Save nanpuyue/7357484426658beeff2aa0919c2c831b to your computer and use it in GitHub Desktop.
将子网掩码转换为前缀长度
#!/bin/bash
# date: 2018-09-15
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# author: nanpuyue <nanpuyue@gmail.com> https://blog.nanpuyue.com
n=0;b=3
for i in ${1//./ };do
if ((0 <= i < 256));then
let n+=$((2**(b*8) * i))
let b--
else
exit 1
fi
done
[[ $(echo "obase=2;$n"|bc) =~ ^(1+)0*$ ]] &&\
((${#BASH_REMATCH[0]} == 32)) &&\
echo ${#BASH_REMATCH[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment