title: Write a bash script to subdivide an given subnet into a pre-defined number of smaller subnets
body: <p>This question was recently asked in an interview.</p>
  
  <p><strong>Question:</strong> Write a bash script to subdivide an given  subnet  into a pre-defined number of smaller subnets.  </p>
  
  <p>After division IP addresses shouldn't be wasted, i.e. accumulation of your subdivisions should make up the divided subnet. </p>
  
  <p>Every subnet has 3 IP addresses reserved and not usable by hosts: network, broadcast, gateway. </p>
  
  <p>Show network/broadcast address, number of hosts and assign gateway. Gateway should be first IP available in divided subnet. Examples: </p>
  
  <p>INPUT: <code>./subnetter.sh  192.168.0.0/24 3</code> </p>
  
  <p>OUTPUT: </p>
  
  <pre><code>subnet=192.168.0.0/25   network=192.168.0.0   broadcast=192.168.0.127 gateway=192.168.0.1   hosts=125 
  subnet=192.168.0.128/26 network=192.168.0.128 broadcast=192.168.0.191 gateway=192.168.0.129 hosts=61 
  subnet=192.168.0.192/26 network=192.168.0.192 broadcast=192.168.0.255 gateway=192.168.0.193 hosts=61
  </code></pre>
  
  <p>INPUT: <code>./subnetter.sh  192.168.0.0/24 4</code></p>
  
  <p>OUTPUT: </p>
  
  <pre><code>subnet=192.168.0.0/26   network=192.168.0.0   broadcast=192.168.0.63  gateway=192.168.0.1   hosts=61 
  subnet=192.168.0.64/26  network=192.168.0.64  broadcast=192.168.0.127 gateway=192.168.0.65  hosts=61 
  subnet=192.168.0.128/26 network=192.168.0.128 broadcast=192.168.0.191 gateway=192.168.0.129 hosts=61 
  subnet=192.168.0.192/26 network=192.168.0.192 broadcast=192.168.0.255 gateway=192.168.0.193 hosts=61
  </code></pre>
  
  <p>INPUT: <code>./subnetter.sh  10.55.10.64/28 2</code> </p>
  
  <p>OUTPUT: </p>
  
  <pre><code>subnet=10.55.10.64/29   network=10.55.10.64   broadcast=10.55.10.71   gateway=10.55.10.65   hosts=5
  subnet=10.55.10.72/29   network=10.55.10.72   broadcast=10.55.10.79   gateway=10.55.10.73   hosts=5
  </code></pre>
  
  <p>First of all, I am trying to analyse what logic is used to divide the subnets.
  Secondly, I am trying to use the <code>ipcalc</code> command to get outputs but no luck.</p>
  
  <p>Thanks</p>