lidia
lidia

Reputation: 3193

shell script + calculate NETWORK IP by shell script

the following question relevant for ksh script

how to calculate the NETWORK IP according to NETMASK & IP ADDRES

if there are some ready shell script to calculate the NETWORK IP

for example

  NETMASK=255.255.255.0
  IP=172.18.20.10


  then NETWORK IP should be 172.18.20.0

lidia

Upvotes: 2

Views: 1926

Answers (1)

Dennis Williamson
Dennis Williamson

Reputation: 360465

Give this a try:

#!/bin/ksh
saveIFS=$IFS
IFS="."
ip=($1)
mask=($2)
for i in {0..3}
do
    (( result[i] = ip[i] & mask[i] ))
done
echo "${result[*]}"
IFS=$saveIFS

Example:

$ ./netip.ksh 172.18.20.10 255.255.255.0
172.18.20.0

Upvotes: 3

Related Questions