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
22__ip_address_detail() {
23	if [ -x /sbin/ip ]; then
24		/sbin/ip -4 addr list
25		/sbin/ip -6 addr list
26	fi
27}
28
29__ip_address() {
30	local interface ipaddr cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ip_address"
31	# Allow interface overrides in $BYOBU_CONFIG_DIR/statusrc
32	if [ -n "$MONITORED_NETWORK" ]; then
33		interface="$MONITORED_NETWORK"
34	else
35		case "$IPV6" in
36			1|true|yes) interface=$(awk '$10 != "lo" { iface=$10 ; }; END { print iface; }' /proc/net/ipv6_route);;
37			*) get_network_interface; interface="$_RET";;
38		esac
39	fi
40	case "$IPV6" in
41		1|true|yes)
42			if [ "$IP_EXTERNAL" = "1" ]; then
43				# Background an update
44				timeout 1 wget -q -O- http://v6.ipv6-test.com/api/myip.php </dev/null >"$cache" 2>/dev/null &
45				sleep 0.02
46			else
47				# Background an update
48				if [ -x /sbin/ip ]; then
49					LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global </dev/null >"$cache" 2>/dev/null &
50				elif eval $BYOBU_TEST ifconfig >/dev/null 2>&1; then
51					LC_ALL=c ifconfig "$interface" | grep "inet6 " | awk '{print $2}' | sed -e "s/%.*//" >"$cache" 2>/dev/null &
52
53				fi
54			fi
55			[ -s "$cache" ] && read ipaddr < "$cache"
56			# Print 'None' if we have no global address
57			[ -z "$ipaddr" ] && ipaddr="None"
58			ipaddr=${ipaddr#* inet6 }
59			ipaddr=${ipaddr%%/*}
60		;;
61		*)
62			if [ "$IP_EXTERNAL" = "1" ]; then
63				timeout 1 wget -q -O- http://v4.ipv6-test.com/api/myip.php </dev/null >"$cache" 2>/dev/null &
64				sleep 0.02
65				[ -s "$cache" ] && read ipaddr < "$cache"
66			elif metadata_available; then
67				# We're in EC2, so get our public IP address
68				timeout 0.2 wget -q -O- http://169.254.169.254/latest/meta-data/public-ipv4 </dev/null >"$cache" 2>/dev/null &
69				sleep 0.02
70				[ -s "$cache" ] && read ipaddr < "$cache"
71			else
72				if [ -x /sbin/ip ]; then
73					ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global 2>/dev/null)
74					ipaddr=${ipaddr#* inet }
75					ipaddr=${ipaddr%%/*}
76				elif eval $BYOBU_TEST ifconfig >/dev/null 2>&1; then
77					ipaddr=$(ifconfig "$interface" | grep "inet " | awk '{print $2}')
78				fi
79			fi
80		;;
81	esac
82	if [ -n "$ipaddr" ]; then
83		if [ "$1" = "t" ]; then
84			printf "%s" "$ipaddr"
85		else
86			color b w k; printf "%s" "$ipaddr"; color --
87		fi
88	fi
89}
90
91# vi: syntax=sh ts=4 noexpandtab
92