1#!/bin/sh 2 3################################################################################ 4## ## 5## Copyright (c) International Business Machines Corp., 2005 ## 6## ## 7## This program is free software; you can redistribute it and#or modify ## 8## it under the terms of the GNU General Public License as published by ## 9## the Free Software Foundation; either version 2 of the License, or ## 10## (at your option) any later version. ## 11## ## 12## This program is distributed in the hope that it will be useful, but ## 13## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## 14## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## 15## for more details. ## 16## ## 17## You should have received a copy of the GNU General Public License ## 18## along with this program; if not, write to the Free Software ## 19## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## 20## ## 21## ## 22################################################################################ 23# 24# File: 25# find_portbundle 26# 27# Description: 28# Find a bundle of consecutive ports 29# 30# Arguments: 31# $1: tcp or udp 32# $2: port which is the start point to check 33# $3: quantity of the ports 34# 35# Returns: 36# 0: Success 37# 1: Fail 38# 39# Author: 40# Mitsuru Chinen <mitch@jp.ibm.com> 41# 42# History: 43# Oct 19 2005 - Created (Mitsuru Chinen) 44# 45#----------------------------------------------------------------------- 46#Uncomment line below for debug output. 47#trace_logic=${trace_logic:-"set -x"} 48$trace_logic 49 50# Make sure the value of LTPROOT 51LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`} 52export LTPROOT 53 54 55# Check argument 56if [ $# -ne 3 ] ; then 57 echo "Usage: find_portbundle protocol start_port quantity_of_ports" >&2 58 exit 1 59fi 60protocol=$1 61start_port=$2 62port_quantity=$3 63 64# Specify the option of the nestat command 65case $protocol in 66 tcp) 67 proto_opt="-t" 68 ;; 69 udp) 70 proto_opt="-u" 71 ;; 72 *) 73 echo "$protocol is not supported" >&2 74 exit 1 75 ;; 76esac 77 78# Create the open port list 79port_list=`mktemp` 80netstat -a -n ${proto_opt} \ 81 | tail -n +3 \ 82 | awk '{ print $4 }' \ 83 | sed 's/^.*://' \ 84 | sort -n \ 85 | uniq > ${port_list} 86 87# Search the available ports 88skip=1 89min_port=$start_port 90max_port=`expr $min_port + $port_quantity - 1` 91if [ ${max_port} -gt 65535 ]; then 92 rm -f $port_list 93 exit 1 94fi 95 96while read line ; do 97 # Skip to the required port 98 if [ $skip -eq 1 ]; then 99 if [ $line -lt $start_port ]; then 100 continue 101 else 102 skip=0 103 fi 104 fi 105 106 # If required quantity of the port isn't available, seach in the next range 107 if [ $line -le $max_port ]; then 108 min_port=`expr $line + 1` 109 max_port=`expr $min_port + $port_quantity - 1` 110 if [ $max_port -gt 65535 ]; then 111 rm -f $port_list 112 exit 1 113 fi 114 continue 115 fi 116 break 117done < $port_list 118 119rm -f $port_list 120 121# Print the result. If required quantity is not 1, print in range expression 122if [ $min_port -eq $max_port ]; then 123 echo "$min_port" 124else 125 echo "$min_port-$max_port" 126fi 127 128exit 0 129