find_portbundle 3.6 KB

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