Created
April 26, 2011 18:15
-
-
Save jaysoffian/7bb70e1065f085b46a00 to your computer and use it in GitHub Desktop.
network calculator in awk
This file contains 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
netfn () { | |
echo "$1" | | |
awk 'function nto32b(n){ # converts an integer to a 32-bit ascii bitstring | |
b="" | |
while (n>1) { | |
if (n%2) {b="1"b} else {b="0"b} | |
n=int(n/2) | |
} | |
b="1"b | |
return sprintf("%032s",b) | |
} | |
function ntoa(n){ # converts an integer to a dotted-quad | |
return b32toa(nto32b(n)) | |
} | |
function aton(a){ # converts a dotted-quad to an integer | |
split(a,p,"\.") | |
return(p[1]*256^3+p[2]*256^2+p[3]*256+p[4]) | |
} | |
function b8toa(bs) { # turns an 8-bit ascii bitstring into a byte | |
n=0 | |
for(i=1;i<=8;i++) { | |
n*=2; n+=substr(bs,i,1) | |
} | |
return n | |
} | |
function b32toa(bs) { # turns a 32-bit ascii bitstring into a dotted-quad address | |
return sprintf("%d.%d.%d.%d", | |
b8toa(substr(bs,1 ,8)), | |
b8toa(substr(bs,9 ,8)), | |
b8toa(substr(bs,17,8)), | |
b8toa(substr(bs,25,8))) | |
} | |
function band(j,k){ # performs the bitwise-and of two ascii bitstrings and returns an ascii bitstring | |
b="" | |
for(i=1;i<=length(j);i++) { | |
if (substr(j,i,1)=="1"&&substr(k,i,1)=="1"){b=b"1"}else{b=b"0"} | |
} | |
return b | |
} | |
function bor(j,k){ # performs the bitwise-or of two ascii bitstrings and returns an ascii bitstring | |
b="" | |
for(i=1;i<=length(j);i++) { | |
if (substr(j,i,1)=="1"||substr(k,i,1)=="1"){b=b"1"}else{b=b"0"} | |
} | |
return b | |
} | |
function bnot(j){ # performs the bitwise-not of an ascii string | |
b="" | |
for(i=1;i<=length(j);i++) { | |
if (substr(j,i,1)=="1"){b=b"0"}else{b=b"1"} | |
} | |
return b | |
} | |
function bitstomask(i) { # converts N-bits to netmask | |
return ntoa(2^32-2^(32-i)) | |
} | |
function masktobits(a) { # converts netmask to network bitsize | |
return index(nto32b(aton(a)),"0")-1 | |
} | |
/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ { | |
network=b32toa(band(nto32b(aton($1)),nto32b(aton($2)))) | |
defroute=ntoa(aton(network)+1) | |
broadcast=b32toa(bor(nto32b(aton(network)),bnot(nto32b(aton($2))))) | |
printf(" IPADDR=%s\n",$1) | |
printf(" NETMASK=%s\n",$2) | |
printf(" NETWORK=%s\n",network) | |
printf(" NETWORKBITS=%s\n",masktobits($2)) | |
printf("DEFAULTROUTER=%s\n",defroute) | |
printf(" BROADCAST=%s\n",broadcast) | |
next | |
} | |
/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+$/ { | |
split($1,a,"\/") | |
netmask=bitstomask(a[2]) | |
network=b32toa(band(nto32b(aton(a[1])),nto32b(aton(netmask)))) | |
defroute=ntoa(aton(network)+1) | |
broadcast=b32toa(bor(nto32b(aton(network)),bnot(nto32b(aton(netmask))))) | |
printf(" IPADDR=%s\n",a[1]) | |
printf(" NETMASK=%s\n",netmask) | |
printf(" NETWORK=%s\n",network) | |
printf(" NETWORKBITS=%s\n",a[2]) | |
printf("DEFAULTROUTER=%s\n",defroute) | |
printf(" BROADCAST=%s\n",broadcast) | |
next | |
} | |
' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment