threeDigitOctet_k93
- Convert octets of an IP address into 3 digit numbers
- Mt Xia: Technical Consulting Group

This script accepts one or more IP address arguments on the command line and converts each octet into 3 digits, zero filling where necessary. The result is an IP address where each octet is a 3 digit number.

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

An example usage of the "threeDigitOctet" script is conversion of "dig" output:


./threeDigitOctet.ksh $( dig www.mtxia.com | grep '^www.mtxia.com' )
# Invalid Address: www.mtxia.com.
# Invalid Address: 2702
# Invalid Address: IN
# Invalid Address: A
038.113.001.151

The output from the "dig" command piped through "grep" is as follows:


www.mtxia.com.    2702    IN    A    38.113.1.151

This entire output can be used as command line arguments to "threeDigitOctet" and will be parsed looking for valid IP addresses. The non-IP address arguments are identified as invalid and passed over. The "# Invalid Address" lines are sent to STDERR and can be redirected to "/dev/null". The remaining output is the converted IP address.

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 2008 By Dana French, All Rights Reserved
#################################################################
function usagemsg_threeDigitOctet {
  print "
Program: ${1}

Convert each position of a dotted decimal IP address into 
three digits per position, zero filling where necessary.

Usage: ${1##*/} [-?vV] [-drn] IPaddress...   
  Where:
    -v = Verbose mode - displays threeDigitOctet function info
    -V = Very Verbose Mode - debug output displayed
    -d = Display results (default)
    -r = Reverse ordering of octets (useful with dynamic DNS)
    -n = Do not convert each octet to three digits (useful with -r)

Author: Dana French (dfrench@mtxia.com)

Copyright 2008 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 octet into 3 digits,
#### zero filling where necessary.  The result is an IP
#### address where each octet is a 3 digit number.
#### 
#### 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 or reversed
#### 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 threeDigitOctet {
  typeset PROGNAME="${0}"
  typeset VERSION="1.2"
  typeset TRUE="1"
  typeset FALSE="0"
  typeset EXITCODE="0"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset DISPLAY="${TRUE}"
  typeset REVERSE="${FALSE}"
  typeset CONVERT="${TRUE}"
  typeset IPADDRESS=""
  typeset MID

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

  while getopts ":vVdrn" OPTION
  do
      case "${OPTION}" in
          'd') DISPLAY="${TRUE}";;
          'r') REVERSE="${TRUE}";;
          'n') CONVERT="${FALSE}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
        [?:#]) usagemsg_threeDigitOctet "${0}" && return 1 ;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  trap "usagemsg_threeDigitOctet ${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 "# Reverse Order..: ${REVERSE}"
  (( VERBOSE  == TRUE )) && print -u 2 "# Display Results: ${DISPLAY}"

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

  RETCODE="5"
  for IPADDRESS in "${@}"
  do
    if [[ "_${IPADDRESS}" != _+([0-9]).+([0-9]).+([0-9]).+([0-9]) ]]
    then
        print -u 2 -- "# Invalid Address: ${IPADDRESS}"
        continue
    fi

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

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

    if (( CONVERT == TRUE ))
    then
      typeset -Z3 DIG1="${IPADDRESS%%.*}"
      typeset -Z3 DIG2="${MID%.*}"
      typeset -Z3 DIG3="${MID#*.}"
      typeset -Z3 DIG4="${IPADDRESS##*.}"
    fi

    if (( CONVERT == FALSE ))
    then
      typeset DIG1="${IPADDRESS%%.*}"
      typeset DIG2="${MID%.*}"
      typeset DIG3="${MID#*.}"
      typeset DIG4="${IPADDRESS##*.}"
    fi

    NEWIP="${DIG1}.${DIG2}.${DIG3}.${DIG4}"
    (( REVERSE == TRUE )) && NEWIP="${DIG4}.${DIG3}.${DIG2}.${DIG1}"

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

    RETCODE="0"
  done

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

threeDigitOctet "${@}"