ip_address 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/sh -e
  2. #
  3. # ip_address: report a host's ip address
  4. #
  5. # Copyright (C) 2008-2011 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. __ip_address_detail() {
  22. if [ -x /sbin/ip ]; then
  23. /sbin/ip -4 addr list
  24. /sbin/ip -6 addr list
  25. fi
  26. }
  27. __ip_address() {
  28. local interface ipaddr cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ip_address"
  29. # Allow interface overrides in $BYOBU_CONFIG_DIR/statusrc
  30. if [ -n "$MONITORED_NETWORK" ]; then
  31. interface="$MONITORED_NETWORK"
  32. else
  33. case "$IPV6" in
  34. 1|true|yes) interface=$(awk '$10 != "lo" { iface=$10 ; }; END { print iface; }' /proc/net/ipv6_route);;
  35. *) get_network_interface; interface="$_RET";;
  36. esac
  37. fi
  38. case "$IPV6" in
  39. 1|true|yes)
  40. if [ "$IP_EXTERNAL" = "1" ]; then
  41. # Background an update
  42. timeout 1 wget -q -O- http://v6.ipv6-test.com/api/myip.php </dev/null >"$cache" 2>/dev/null &
  43. sleep 0.02
  44. else
  45. # Background an update
  46. if [ -x /sbin/ip ]; then
  47. LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global </dev/null >"$cache" 2>/dev/null &
  48. elif eval $BYOBU_TEST ifconfig >/dev/null 2>&1; then
  49. LC_ALL=c ifconfig "$interface" | grep "inet6 " | awk '{print $2}' | sed -e "s/%.*//" >"$cache" 2>/dev/null &
  50. fi
  51. fi
  52. [ -s "$cache" ] && read ipaddr < "$cache"
  53. # Print 'None' if we have no global address
  54. [ -z "$ipaddr" ] && ipaddr="None"
  55. ipaddr=${ipaddr#* inet6 }
  56. ipaddr=${ipaddr%%/*}
  57. ;;
  58. *)
  59. if [ "$IP_EXTERNAL" = "1" ]; then
  60. timeout 1 wget -q -O- http://v4.ipv6-test.com/api/myip.php </dev/null >"$cache" 2>/dev/null &
  61. sleep 0.02
  62. [ -s "$cache" ] && read ipaddr < "$cache"
  63. elif metadata_available; then
  64. # We're in EC2, so get our public IP address
  65. timeout 0.2 wget -q -O- http://169.254.169.254/latest/meta-data/public-ipv4 </dev/null >"$cache" 2>/dev/null &
  66. sleep 0.02
  67. [ -s "$cache" ] && read ipaddr < "$cache"
  68. else
  69. if [ -x /sbin/ip ]; then
  70. ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global 2>/dev/null)
  71. ipaddr=${ipaddr#* inet }
  72. ipaddr=${ipaddr%%/*}
  73. elif eval $BYOBU_TEST ifconfig >/dev/null 2>&1; then
  74. ipaddr=$(ifconfig "$interface" | grep "inet " | awk '{print $2}')
  75. fi
  76. fi
  77. ;;
  78. esac
  79. if [ -n "$ipaddr" ]; then
  80. if [ "$1" = "t" ]; then
  81. printf "%s" "$ipaddr"
  82. else
  83. color b w k; printf "%s" "$ipaddr"; color --
  84. fi
  85. fi
  86. }
  87. # vi: syntax=sh ts=4 noexpandtab