check_icmpv4_connectivity 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. # check_icmpv4_connectivity
  25. #
  26. # Description:
  27. # Check the ICMPv4 connectivity from a interface to an IPv4 address
  28. #
  29. # Arguments:
  30. # $1: source interface name
  31. # $2: destination IPv4 address
  32. #
  33. # Returns:
  34. # 0: connectivity is good
  35. # 1: connectivity is someting wrong
  36. #
  37. # Author:
  38. # Mitsuru Chinen <mitch@jp.ibm.com>
  39. #
  40. # History:
  41. # Oct 19 2005 - Created (Mitsuru Chinen)
  42. #
  43. #-----------------------------------------------------------------------
  44. #Uncomment line below for debug output.
  45. #trace_logic=${trace_logic:-"set -x"}
  46. $trace_logic
  47. # The max number of ICMP echo request
  48. PING_MAX=10
  49. # Check the arguments
  50. if [ $# -ne 2 ]; then
  51. echo "Usage: $0 source_interface_name destionation_ipv4_address" >&2
  52. exit 1
  53. fi
  54. src_ifname=$1
  55. dst_ipv4addr=$2
  56. cnt=0
  57. while [ $cnt -lt $PING_MAX ]; do
  58. cnt=`expr $cnt + 1`
  59. ping -I $src_ifname -c 1 $dst_ipv4addr -w 1 > /dev/null 2>&1
  60. if [ $? -eq 0 ]; then
  61. exit 0
  62. fi
  63. sleep 1
  64. done
  65. exit 1