Find the answer to your Linux question:
Results 1 to 5 of 5
I'm trying to write a script for work for provisioning servers. It has a few questions and what not. One of the questions: What is the network IP and another ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    24

    IP Script

    I'm trying to write a script for work for provisioning servers. It has a few questions and what not.

    One of the questions: What is the network IP
    and another is What is the block /29, /28 etc.

    If they provide the network and the block you can figure out the gateway, and usable IPs

    Is there a way to have /bin/sh configure the IPs with just that information? So if they provide 127.0.0.1/29 it will calculate 127.0.0.1 is the network 127.0.0.2 is the gateway and 127.0.0.3-127.0.0.7 are usable. However, it can't use the ipcfg-eth0-range0 method because Plesk doesn't like it.

    Is this possible?

    Thanks!

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    you can figure out the gateway
    No, it's provided by the network administrator.

    The following link and many other resources on the net may help you.
    Network Calculators

    Your 127.0.0.0 network is not the best example as it refers to a loopback network.

    Loopback | Ask.com Encyclopedia
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    24
    I know how to calculate it. But I want to teach the script so it does it. Is it possible?

  4. #4
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Not fully tested. might be a starting point?
    You will have to extend it to more site specific.

    Code:
    #!/bin/bash
    
    given_address="127.5.45.17/21"
    
    read a b c d << EOF
    `echo $given_address | cut -d "/" -f 1 | tr "."  " "`
    EOF
    
    i=`echo $given_address | cut -d "/" -f 2`
    
    address[0]=${a}
    address[1]=${b}
    address[2]=${c}
    address[3]=${d}
    
    hexsum[0]=0
    hexsum[1]=1
    hexsum[2]=3
    hexsum[3]=7
    hexsum[4]=15
    hexsum[5]=31
    hexsum[6]=64
    hexsum[7]=127
    hexsum[8]=255
    
    j=0
    for j in {0..3}
    do
      mask[j]=0
    done
    
    j=0
    while ( true )
    do
      if [ $i -gt 8 ] 
      then
        mask[${j}]=${hexsum[8]}
        i=$((i-8))
      else
        mask[${j}]=$((hexsum[8]^hexsum[8-i]))
        break
      fi
      
      j=$((j+1))
    done
    
    j=0
    for j in {0..3}
    do
      network[j]=$((address[j]&mask[j]))
    done
    
    echo "network   = ${network[0]}.${network[1]}.${network[2]}.${network[3]}"
    echo "mask      = ${mask[0]}.${mask[1]}.${mask[2]}.${mask[3]}"
    echo "broadcast = $((network[0] + ~mask[0]&255)).$((network[1] + ~mask[1]&255)).$((network[2] + ~mask[2]&255)).$((network[3] + ~mask[3]&255))"
    echo "first     = ${network[0]}.${network[1]}.${network[2]}.${network[3]+1}"
    echo "last      = $((network[0] + ~mask[0]&255)).$((network[1] + ~mask[1]&255)).$((network[2] + ~mask[2]&255)).$((network[3] + ~mask[3]&255 -1))"
    In a world without walls and fences, who needs Windows and Gates?

  5. #5
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Also have a look at ipcalc (part of initscripts in fedora)

    eg.


    Code:
    [guest@fwall tmp]$ ipcalc -n -b -m  127.0.0.1/29
    NETMASK=255.255.255.248
    BROADCAST=127.0.0.7
    NETWORK=127.0.0.0
    In a world without walls and fences, who needs Windows and Gates?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...