Created
May 24, 2016 14:21
-
-
Save LadyNamedLaura/0045e48dbb95d2575a5231b98f487d19 to your computer and use it in GitHub Desktop.
ipcalc with bash
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 | |
nuls='00000000000000000000000000000000' | |
ones='11111111111111111111111111111111' | |
if [ -n "$2" ] | |
then | |
varprefix=${2}_ | |
fi | |
ip=( $( echo $1 | sed 's/[.\/]/ /g') ) | |
ipbin=$(printf "%08d%08d%08d%08d" $(echo "ibase=10;obase=2;${ip[0]};${ip[1]};${ip[2]};${ip[3]};" | bc)) | |
cdir=${ip[4]} | |
bintoip() { | |
addr=($(echo $1 | sed 's/\([01]\{8\}\)\([01]\{8\}\)\([01]\{8\}\)\([01]\{8\}\)/\1 \2 \3 \4/' )) | |
echo "obase=10;ibase=2;${addr[0]};${addr[1]};${addr[2]};${addr[3]};" | bc | tr '\n' '.' | cut -d '.' -f 1-4 | |
} | |
echo ${varprefix}ip=${ip[0]}.${ip[1]}.${ip[2]}.${ip[3]} | |
echo ${varprefix}cdir=$cdir | |
echo -n "${varprefix}netmask=" | |
bintoip ${ones:0:$cdir}${nuls:$cdir} | |
echo -n "${varprefix}netaddr=" | |
bintoip ${ipbin:0:$cdir}${nuls:$cdir} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect