get_ifname 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. # get_ifname
  25. #
  26. # Description:
  27. # Get the interface name which belongs to the specified test link
  28. #
  29. # Author:
  30. # Mitsuru Chinen <mitch@jp.ibm.com>
  31. #
  32. # Arguments:
  33. # $1: Set the host type to set the IPv4 address
  34. # lhost - local host / rhost - remote host
  35. # $2: The number of the test link
  36. #
  37. # History:
  38. # Oct 19 2005 - Created (Mitsuru Chinen)
  39. #
  40. #-----------------------------------------------------------------------
  41. #Uncomment line below for debug output.
  42. #trace_logic=${trace_logic:-"set -x"}
  43. $trace_logic
  44. # Make sure the value of LTPROOT
  45. LTPROOT=${LTPROOT:-`(cd ../../ ; pwd)`}
  46. TMPDIR=${TMPDIR:-/tmp}
  47. export LTPROOT TMPDIR
  48. # Check the environment variable for the test
  49. . check_envval || exit 1
  50. # Arguments
  51. if [ $# -ne 2 ]; then
  52. echo "Usage: $0 host_type link_num" >&2
  53. exit 1
  54. fi
  55. host_type=$1
  56. link_num=$2
  57. # Check the host type
  58. case $host_type in
  59. lhost)
  60. hwaddrs="$LHOST_HWADDRS"
  61. ;;
  62. rhost)
  63. hwaddrs="$RHOST_HWADDRS"
  64. ;;
  65. *)
  66. echo "$0: 1st argument must be lhost or rhost" >&2
  67. exit 1
  68. ;;
  69. esac
  70. # Pick HWaddr from HWaddr list
  71. field=`expr $link_num + 1`
  72. hwaddr=`echo $hwaddrs | cut -d ' ' -f $field`
  73. if [ x${hwaddr} = x ]; then
  74. echo "HWaddr list ($hwaddrs) is something wrong." >&2
  75. exit 1
  76. fi
  77. ip_link_show_out=`mktemp $TMPDIR/tmp.XXXXXXXX`
  78. if [ $host_type = lhost ]; then
  79. ip link show > $ip_link_show_out 2>&1
  80. else
  81. $LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH ip link show' \
  82. > $ip_link_show_out 2>&1
  83. fi
  84. ifname=`grep -1 -i $hwaddr $ip_link_show_out | head -n 1 | awk '{ print $2 }' | sed "s/://"` 2>/dev/null
  85. rm -f $ip_link_show_out
  86. # Detect a interface name from the HWaddr
  87. if [ x$ifname = x ]; then
  88. echo "Interface which has $hwaddr is not found." >&2
  89. exit 1
  90. fi
  91. echo $ifname
  92. exit 0