| 1 | #!/bin/bash |
| 2 | ###################################################### |
| 3 | # |
| 4 | # Author: Ceasar Sun Chen-kai <[email protected]> |
| 5 | # License: GPL |
| 6 | # Description: List IP addresses from a set of given subnet and count |
| 7 | # Usage: ipcalc-list 192.168.21.13/24 -n 10 # to list ip from 192.168.21.13 ~ 192.168.21.22 |
| 8 | # ChangeLog: |
| 9 | # * 20100603 First version |
| 10 | # * 20100604 Fix fisrt character of binary type ip address is 0 within first ip session |
| 11 | # * 20140321 Add option "-r" |
| 12 | ###################################################### |
| 13 | # Load DRBL setting and functions |
| 14 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 15 | |
| 16 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 17 | [ -e /etc/drbl/drbl-ocs.conf ] && . /etc/drbl/drbl-ocs.conf |
| 18 | [ -e $DRBL_SCRIPT_PATH/sbin/ocs-functions ] && . $DRBL_SCRIPT_PATH/sbin/ocs-functions |
| 19 | |
| 20 | ###################################################### |
| 21 | # Sub functions |
| 22 | ###################################################### |
| 23 | |
| 24 | # Print usage |
| 25 | Usage(){ |
| 26 | echo "$0 : List legal IP addresses from a given start IP/subnet and count"; |
| 27 | echo "Usage: $0 <ipcalc_parameters> -c <count> [-b] [-v]"; |
| 28 | echo " -b, --binary Binary output for IP address" |
| 29 | echo " -c,--count [COUNT] Amount of IP address, positive number for ascending order, negative for descending, ex: 10, -5" |
| 30 | echo " -r, --range <START_IP END_IP NETMASK> Assign IP range and netmask" |
| 31 | echo " -v, --verbose Verbose mode, if not enough IP address, show what we have" |
| 32 | echo "" |
| 33 | echo "Help options:"; |
| 34 | echo " -h, --help Show this help message" |
| 35 | echo " " |
| 36 | echo "Example:" |
| 37 | echo "To list 20 ascending legal IP addresses in form 192.168.121.1/24." |
| 38 | echo " $0 192.168.121.1/24 -c 20" |
| 39 | echo "To list 3 legal descending IP addresses in form 172.16.1.32/16 and convert IP address as binary." |
| 40 | echo " $0 172.16.1.32 255.255.0.0 -c -3 -b" |
| 41 | echo "To list 10 descnding IP addresses in form 10.0.0.3/8. Nothing will be shown due to not enough addresses in the range" |
| 42 | echo " $0 10.0.0.3/8 -c -10" |
| 43 | echo "But use VERBOSE mode. Output: '10.0.0.3 10.0.0.2 10.0.0.1'" |
| 44 | echo " $0 10.0.0.3/8 -c -10 -v" |
| 45 | echo "To list all IP addresss form 172.16.0.1 to 172.16.1.46 with netmask 16. Output: '172.16.0.1 172.16.0.2 ... 172.16.1.45 172.16.1.46'" |
| 46 | echo " $0 -r 172.16.0.1 172.16.1.46 255.255.0.0 (or: $0 -r 172.16.0.1 172.16.1.46 16)" |
| 47 | echo "" |
| 48 | echo "ipcalc_parameters : <start_ip netmask> or <start_ip/netmask>" |
| 49 | echo " <start_ip netmask> , ex: 192.168.11.1 255.255.255.0" |
| 50 | echo "the same with " |
| 51 | echo " <start_ip/netmask> , ex: 192.168.11.1/24" |
| 52 | } |
| 53 | |
| 54 | ###################################################### |
| 55 | #Main |
| 56 | ###################################################### |
| 57 | declare _OUTPUT_MODE _VERBOSE _COUNT _IPCALC_PARM _IP_START _IP_END _NETMASK |
| 58 | declare -a _IP_ARRAY |
| 59 | |
| 60 | declare __VERSION='1.0.0' |
| 61 | declare _OUTPUT_MODE=d |
| 62 | declare _VERBOSE=0 |
| 63 | declare _COUNT=1 |
| 64 | |
| 65 | [ $# -eq 0 ] && Usage && exit 1; |
| 66 | while [ $# -gt 0 ]; do |
| 67 | case "$1" in |
| 68 | -b|--binary) |
| 69 | shift; _OUTPUT_MODE="b"; |
| 70 | ;; |
| 71 | -c|--count) |
| 72 | shift; |
| 73 | if [[ $1 =~ ^-*[[:digit:]]+$ ]]; then |
| 74 | _COUNT=$1 |
| 75 | else |
| 76 | echo "Illegal value for option \"-c\"." >&2 |
| 77 | Usage; exit 1; |
| 78 | fi |
| 79 | shift; |
| 80 | ;; |
| 81 | -h|--help) |
| 82 | Usage; |
| 83 | exit; |
| 84 | ;; |
| 85 | -r|--range) |
| 86 | shift; |
| 87 | if [[ $1 =~ ^[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}$ ]] ; then |
| 88 | _IP_START=$1 |
| 89 | else |
| 90 | echo "Illegal 'Start' IP format !?" >&2 |
| 91 | Usage; exit 1; |
| 92 | fi |
| 93 | shift; |
| 94 | if [[ $1 =~ ^[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}$ ]] ; then |
| 95 | _IP_END=$1 |
| 96 | else |
| 97 | echo "Illegal 'End' IP format !?" >&2 |
| 98 | Usage; exit 1; |
| 99 | fi |
| 100 | shift; |
| 101 | if [[ $1 =~ ^[[:digit:]]{1,2}$ ]] && [ $1 -le 30 ] ; then |
| 102 | _NETMASK=$1 |
| 103 | elif [[ $1 =~ ^[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}$ ]] ; then |
| 104 | _NETMASK=$1 |
| 105 | else |
| 106 | echo "Illegal netmask format !?" >&2 |
| 107 | Usage; exit 1; |
| 108 | fi |
| 109 | shift; |
| 110 | ;; |
| 111 | -v|--verbose) |
| 112 | shift; _VERBOSE=1; |
| 113 | ;; |
| 114 | *) |
| 115 | [ -n "$_IPCALC_PARM" ] && _IPCALC_PARM="$_IPCALC_PARM $1" || _IPCALC_PARM="$1"; |
| 116 | shift; |
| 117 | ;; |
| 118 | esac |
| 119 | done |
| 120 | |
| 121 | # chek if ipcalc_parameters format : '192.168.11.1 255.255.255.0' or '192.168.11.1/24' |
| 122 | # or not list a range of IPs |
| 123 | |
| 124 | if [ -n "$_IP_START" -a -n "$_IP_END" ] ; then |
| 125 | real_bin_start_ip="$(drbl-ipcalc $_IP_START/0 | grep -E "^Address:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 126 | real_dec_start_ip="$(LC_ALL=C echo "ibase=2; obase=A; $real_bin_start_ip" | bc)" |
| 127 | real_bin_end_ip="$(drbl-ipcalc $_IP_END/0 | grep -E "^Address:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 128 | real_dec_end_ip="$(LC_ALL=C echo "ibase=2; obase=A; $real_bin_end_ip" | bc)" |
| 129 | |
| 130 | [ "$(expr $real_dec_end_ip - $real_dec_start_ip)" -ge 0 ] && _COUNT="$(expr $real_dec_end_ip - $real_dec_start_ip + 1)" || _COUNT="$(expr $real_dec_end_ip - $real_dec_start_ip - 1)" |
| 131 | _IPCALC_PARM="${_IP_START}/${_NETMASK}" |
| 132 | _VERBOSE=1 |
| 133 | fi |
| 134 | #echo "_IPCALC_PARM='$_IPCALC_PARM' ; _COUNT='$_COUNT' " |
| 135 | |
| 136 | if [ -z "$(echo $_IPCALC_PARM | grep -E '^[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}\/[[:digit:]]{1,2}$')" ] && [ -z "$(echo $_IPCALC_PARM | grep -E '^[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}[[:space:]]+[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}.[[:digit:]]{1,3}$')" ] && [ -z "$_IP_START" -o -z "$_IP_END" ]; then |
| 137 | echo "Illegal parameters for ipcalc!" >&2 |
| 138 | Usage; exit 1; |
| 139 | fi |
| 140 | |
| 141 | # Add "1" on first string for solve if the fisrt session of ip is 0xxxxxxx.~ |
| 142 | # |
| 143 | # Exit when it gave an invild netmask |
| 144 | [[ -n "$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^INVALID MASK")" ]] && echo "Illegal parameters for netmask:'$_IPCALC_PARM'" >&2 && exit 1; |
| 145 | |
| 146 | bin_start_ip=1"$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^Address:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 147 | dec_start_ip="$(LC_ALL=C echo "ibase=2; obase=A; $bin_start_ip" | bc)" |
| 148 | bin_max_ip=1"$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^HostMax:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 149 | dec_max_ip="$(LC_ALL=C echo "ibase=2; obase=A; $bin_max_ip" | bc)" |
| 150 | bin_min_ip=1"$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^HostMin:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 151 | dec_min_ip="$(LC_ALL=C echo "ibase=2; obase=A; $bin_min_ip" | bc)" |
| 152 | |
| 153 | bin_network_ip=1"$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^Network:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 154 | dec_network_ip="$(LC_ALL=C echo "ibase=2; obase=A; $bin_network_ip" | bc)" |
| 155 | bin_broadcast_ip=1"$(LC_ALL=C drbl-ipcalc $_IPCALC_PARM | grep -E "^Broadcast:" | awk -F" " '{print $3 $4 }'| sed -e "s/\.//g")" |
| 156 | dec_broadcast_ip="$(LC_ALL=C echo "ibase=2; obase=A; $bin_broadcast_ip" | bc)" |
| 157 | |
| 158 | #echo "$_OUTPUT_MODE, $_VERBOSE, $_COUNT, '$_IPCALC_PARM', $dec_start_ip, $dec_min_ip, $dec_max_ip"; |
| 159 | |
| 160 | if [[ $_COUNT =~ ^[[:digit:]]+$ ]]; then |
| 161 | for ((i=0; i<$_COUNT; i++)) ;do |
| 162 | dec_current_ip=$(expr $dec_start_ip + $i) |
| 163 | if [ $dec_max_ip -ge $dec_current_ip ] && [ $dec_min_ip -le $dec_current_ip ] && [ $dec_network_ip -ne $dec_current_ip ] && [ $dec_broadcast_ip -ne $dec_current_ip ]; then |
| 164 | _tmp_bin_ip="$(LC_ALL=C echo "ibase=A; obase=2; $dec_current_ip" | bc)" |
| 165 | _IP_ARRAY[${#_IP_ARRAY[*]}]=${_tmp_bin_ip:1:32} |
| 166 | elif [ $_VERBOSE = 0 ] ; then |
| 167 | _IP_ARRAY=() ; echo "Not enough IP addresses to output!" >&2 |
| 168 | exit 1; |
| 169 | fi |
| 170 | done |
| 171 | else |
| 172 | for ((i=0; i>$_COUNT; i--)) ;do |
| 173 | dec_current_ip=$(expr $dec_start_ip + $i) |
| 174 | if [ $dec_max_ip -ge $dec_current_ip ] && [ $dec_min_ip -le $dec_current_ip ] && [ $dec_network_ip -ne $dec_current_ip ] && [ $dec_broadcast_ip -ne $dec_current_ip ]; then |
| 175 | _tmp_bin_ip=`echo "ibase=A; obase=2; $dec_current_ip" | bc` |
| 176 | _IP_ARRAY[${#_IP_ARRAY[*]}]=${_tmp_bin_ip:1:32} |
| 177 | elif [ $_VERBOSE = 0 ] ; then |
| 178 | _IP_ARRAY=() ; echo "Not enough IP addresses to output!" >&2 |
| 179 | exit 1; |
| 180 | fi |
| 181 | done |
| 182 | fi |
| 183 | |
| 184 | for ((i=0; i<${#_IP_ARRAY[*]}; i++)) ;do |
| 185 | [ $_OUTPUT_MODE = 'b' ] && p1=${_IP_ARRAY[$i]:0:8} || p1="$(echo "ibase=2; obase=A; ${_IP_ARRAY[$i]:0:8}" | bc )" |
| 186 | [ $_OUTPUT_MODE = 'b' ] && p2=${_IP_ARRAY[$i]:8:8} || p2="$(echo "ibase=2; obase=A; ${_IP_ARRAY[$i]:8:8}" | bc )" |
| 187 | [ $_OUTPUT_MODE = 'b' ] && p3=${_IP_ARRAY[$i]:16:8} || p3="$(echo "ibase=2; obase=A; ${_IP_ARRAY[$i]:16:8}" | bc )" |
| 188 | [ $_OUTPUT_MODE = 'b' ] && p4=${_IP_ARRAY[$i]:24:8} || p4="$(echo "ibase=2; obase=A; ${_IP_ARRAY[$i]:24:8}" | bc )" |
| 189 | echo $p1.$p2.$p3.$p4 |
| 190 | done |
| 191 | |
| 192 | exit 0; |