Simple IP calculator Subnet using korn shell script v1.0

ComputerCarriage > Posts > Commands > Simple IP calculator Subnet using korn shell script v1.0
ip calculator subnet

IP Calculator Subnet

Introduction

In this post we are going to see how to write a script to find the subnet calculator, we are using korn shell to write. the interpreter we are going to use /bin/ksh, the korn shell supports MacOS, Linux and Unix flavours. This will be helpful for assign the ip address to the server ip planning.

Script

The below script is the IP calculator subnet script

#!/bin/ksh

typeset -i2 mask=255

[[ $# != 2 ]] && {

   echo "Usage: $0 ipaddress subnetmask"

   exit 1

}

SaveIFS=$IFS

IFS=.

typeset -a IParrn=($1)

typeset -a NMarr=($2)

IFS=$SaveIFS

typeset -i2 ip1=${IParrn[0]}

typeset -i2 ip2=${IParrn[1]}

typeset -i2 ip3=${IParrn[2]}

typeset -i2 ip4=${IParrn[3]}

typeset -i2 nm1=${NMarr[0]}

typeset -i2 nm2=${NMarr[1]}

typeset -i2 nm3=${NMarr[2]}

typeset -i2 nm4=${NMarr[3]}

echo

echo "       IP Address: $1"

echo "      Subnet Mask: $2"

echo "  Network Address: $((ip1 & nm1)).$((ip2 & nm2)).$((ip3 & nm3)).$((ip4 & nm4))"

echo "Broadcast Address: $((ip1 | (mask ^ nm1))).$((ip2 | (mask ^ nm2))).$((ip3 | (mask ^ nm3))).$((ip4 | (mask ^ nm4)))"

echo

exit 0

Execute the script

Execute the script in any Unix platform, make sure ksh is installed and make sure execute permission is applied for the ipcalc.sh file

Example 1:

The below example shows the IP Address, subnet mask, network address and broadcast address of the IP provided.

server@control Desktop % ./ipcalc.sh 10.10.0.1 255.255.0.0

       IP Address: 10.10.0.1
      Subnet Mask: 255.255.0.0
  Network Address: 10.10.0.0
Broadcast Address: 10.10.255.255

server@control Desktop % 

Example 2:

The below example for the class 2 ip address subnet

server@control Desktop % ./ipcalc.sh 192.168.0.1 255.255.0.0  

       IP Address: 192.168.0.1
      Subnet Mask: 255.255.0.0
  Network Address: 192.168.0.0
Broadcast Address: 192.168.255.255

server@control Desktop %

See Aslo

Korn shell wiki – https://en.wikipedia.org/wiki/KornShell

Shell Script basics – https://computercarriage.com/2020/12/06/shell-script-basics/

Hope you got some idea on the ip calculator subnet and how to use it. let me know your comments on the same.

One thought on “Simple IP calculator Subnet using korn shell script v1.0

Leave a Reply