integerIPAddress_k93
- Convert a dotted decimal IP address into a base10 integer number
- Mt Xia: Technical Consulting Group

This script accepts one or more IP address arguments on the command line and converts each into a base10 integer number.

Example: Converts an IP address such as "10.1.100.50" to "167863346"

An example usage of the "integerIPAddress" script:


./integerIPAddress.ksh -d 10.1.100.50 38.113.1.151 72.14.204.103
10.1.100.50:167863346
38.113.1.151:644940183
72.14.204.103:1208929383

We have expert shell programmers currently available to assist your organization with complex shell scripting projects. We have developed business continuity, disaster recovery, high availability and virtualization systems entirely in shell scripts and we have the advanced technical skills to solve your problems as well. Please Contact Us for more information.



#!/usr/bin/ksh93
################################################################
#### Copyright 2010 By Dana French, All Rights Reserved
#################################################################
function usagemsg_integerIPAddress {
  print "
Program: ${1}

Convert one or more dotted decimal IP address into a 
base10 integer number.  Also converts base10 integer
numbers into dotted decimal IP addresses.

Usage: ${1##*/} [-?vV] IPaddress...   
  Where:
    -v = Verbose mode - displays integerIPAddress function info
    -V = Very Verbose Mode - debug output displayed

Author: Dana French (dfrench@mtxia.com)

Copyright 2010 By Dana French, All Rights Reserved

\"AutoContent\" enabled
"
}
################################################################
#### 
#### Description:
#### This script accepts one or more IP address arguments on
#### the command line and converts each address into a base10
#### integer number that is resolvable as the IP address.
#### 
#### Assumptions:
#### 
#### Each command line specified IP address is assumed to
#### consist of 4 octets separated with a period(.).  Each
#### octet is assumed to be a 1, 2, or 3 digit number between
#### zero (0) and 255.
#### 
#### Dependencies:
#### 
#### The only dependency is a recent version of Korn Shell
#### 93, however it does appear to also work with bash.
#### 
#### Products:
#### 
#### The output of this script is the converted
#### IP address(es) specifed on the command line, each
#### address followed by a newline.
#### 
#### Configured Usage:
#### 
#### This script can be run from the command line or included
#### in a library and called as a function.  One or more IP
#### addresses must be specified on the command line or an
#### error is generated.
#### 
#### Details:
#### 
################################################################
function integerIPAddress {
  typeset PROGNAME="${0}"
  typeset VERSION="1.0"
  typeset TRUE="1"
  typeset FALSE="0"
  typeset EXITCODE="0"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset DISPLAY="${TRUE}"
  typeset IPADDRESS=""
  typeset INTEGER=""
  typeset DTOI="${TRUE}"
  typeset ITOD="${FALSE}"
  typeset MID
  typeset DIG1
  typeset DIG2
  typeset DIG3
  typeset DIG4
  typeset BIN1
  typeset BIN2
  typeset BIN3
  typeset BIN4

#### 
#### Process the command line options and arguments, saving
#### the values as appropriate.
#### 

  while getopts ":vVdio" OPTION
  do
      case "${OPTION}" in
          'd') DTOI="${TRUE}"
               ITOD="${FALSE}";;
          'i') ITOD="${TRUE}"
               DTOI="${FALSE}";;
          'o') DISPLAY="${TRUE}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
        [?:#]) usagemsg_integerIPAddress "${0}" && return 1 ;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  trap "usagemsg_integerIPAddress ${0}" EXIT

  if (( ${#@} == 0 ))
  then
    print -u 2 "# ERROR: IP Address(es) not specified"
    return 2
  fi

  trap "-" EXIT
  
################################################################

#### 
#### Display some program info and the command line arguments specified
#### if "VERBOSE" mode was specified.
#### 

  (( VERYVERB == TRUE )) && set -x
  (( VERBOSE  == TRUE )) && print -u 2 "# Program Name...: ${PROGNAME}"
  (( VERBOSE  == TRUE )) && print -u 2 "# Version........: ${VERSION}"
  (( VERBOSE  == TRUE )) && print -u 2 "# True...........: ${TRUE}"
  (( VERBOSE  == TRUE )) && print -u 2 "# False  ........: ${FALSE}"
  (( VERBOSE  == TRUE )) && print -u 2 "# Display Results: ${DISPLAY}"
  (( VERBOSE  == TRUE )) && print -u 2 "# DotDec to Int..: ${DTOI}"
  (( VERBOSE  == TRUE )) && print -u 2 "# Int to DotDec..: ${ITOD}"

################################################################

  RETCODE="5"
  for IPADDRESS in "${@}"
  do

    unset BINIP DOTIP DECIP
    unset MID DIG1 DIG2 DIG3 DIG4
    unset BIN1 BIN2 BIN3 BIN4

    (( VERBOSE  == TRUE )) && print -u 2 "# IP Address.....: ${IPADDRESS}"

    if [[ "_${IPADDRESS}" == _+([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]
    then
      DOTIP="${IPADDRESS}"
      DTOI="${TRUE}"
      ITOD="${FALSE}"

    elif [[ "_${IPADDRESS}" == _+([0-9]) ]]
    then
      DECIP="${IPADDRESS}"
      DTOI="${FALSE}"
      ITOD="${TRUE}"

    else
      DTOI="${FALSE}"
      ITOD="${FALSE}"
      print -u 2 -- "# Invalid Address Format: ${IPADDRESS}"
      continue
    fi

    (( DTOI == TRUE )) && TORF="TRUE" || TORF="FALSE"
    (( VERBOSE  == TRUE )) && print -u 2 "# DotDec to Int..: ${TORF}"
    (( ITOD == TRUE )) && TORF="TRUE" || TORF="FALSE"
    (( VERBOSE  == TRUE )) && print -u 2 "# Int to DotDec..: ${TORF}"

####
# Convert Dotted Decimal to base10 integer value

    if (( DTOI == TRUE ))
    then

      MID="${IPADDRESS#*.}"
      MID="${MID%.*}"
      typeset DIG1="${IPADDRESS%%.*}"
      typeset DIG2="${MID%.*}"
      typeset DIG3="${MID#*.}"
      typeset DIG4="${IPADDRESS##*.}"

      typeset -Z8 BIN1=$( print -- "ibase=A;obase=2;${DIG1}" | bc )
      typeset -Z8 BIN2=$( print -- "ibase=A;obase=2;${DIG2}" | bc )
      typeset -Z8 BIN3=$( print -- "ibase=A;obase=2;${DIG3}" | bc )
      typeset -Z8 BIN4=$( print -- "ibase=A;obase=2;${DIG4}" | bc )

      BINIP="${BIN1}${BIN2}${BIN3}${BIN4}"
      DECIP=$( print -- "obase=A;ibase=2;${BINIP}" | bc )

      RETCODE="0"
    fi

####
# Convert base10 integer to Dotted Decimal value

    if (( ITOD == TRUE ))
    then

      typeset -Z32 BINIP=$( print -- "obase=2;ibase=A;${IPADDRESS}" | bc )

      BIN1="${BINIP:0:8}"
      BIN2="${BINIP:8:8}"
      BIN3="${BINIP:16:8}"
      BIN4="${BINIP:24:8}"

      typeset -Z3 DIG1=$( print -- "ibase=2;obase=A;${BIN1}" | bc )
      typeset -Z3 DIG2=$( print -- "ibase=2;obase=A;${BIN2}" | bc )
      typeset -Z3 DIG3=$( print -- "ibase=2;obase=A;${BIN3}" | bc )
      typeset -Z3 DIG4=$( print -- "ibase=2;obase=A;${BIN4}" | bc )

      DOTIP="${DIG1}.${DIG2}.${DIG3}.${DIG4}"

      RETCODE="0"
    fi

####

# Display results if specified on the command line
    if (( VERBOSE  == TRUE )) ||
       (( DISPLAY  == TRUE ))
    then
      (( VERBOSE  == TRUE )) && print -u 2 -- "# Binary Result..: ${BINIP}"
      (( VERBOSE  == TRUE )) && print -u 2 -- "# DotDec Result..: ${DOTIP}"
      (( VERBOSE  == TRUE )) && print -u 2 -- "# Decimal Result.: ${DECIP}"
      (( DISPLAY  == TRUE )) && print "${DOTIP}:${DECIP}"
    fi

  done

  return ${RETCODE}
}
################################################################

integerIPAddress "${@}"