Skip to content

Instantly share code, notes, and snippets.

@nanpuyue nanpuyue/mask_to_prefix.sh
Last active Sep 15, 2018

Embed
What would you like to do?
将子网掩码转换为前缀长度
#!/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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.