#!/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 "${@}"