Hi Brett. Thanks for the tip. Unfortunately, `lips` didn't quite work for me. For some reason, my local IP address was represented on en1, not en0. To address this, I thought I'd try to scan a range of devices by using the following at the start of the `lips` function:
lips() {
local ip
for num in $(seq 9)
do
ip=$(ifconfig en${num} 2>/dev/null | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}')
[[ -n "$ip" ]] && break
done
local locip extip
...
I frequently (and intentionally) have multiple network connections. Here's another way of approaching the same thing that may be slightly cleaner:
function ips () {
# List IP addresses for each active interface
local interface
local interfaces=$(networksetup -listallhardwareports | awk '/^Device: /{print $2}')
for interface in $interfaces
do
local ip=$(ipconfig getifaddr $interface)
[ "$ip" != "" ] && printf "%11s: %s\n" "$interface" "$ip"
done
}
Hi Daniel. Thanks for posting about networksetup. I had no idea that tool existed. It looks pretty useful. After playing around with it for a bit, I came up with another version of lips. This one incorporates the "human-readable" version of the network interface so that you can see things like "WiFi", "Ethernet", etc:
lips ()
{
local KEY VALUE PORT DEVICE IP EXTIP;
while IFS=':' read KEY VALUE; do
VALUE="${VALUE:1}"; # Remove leading space
[[ "$KEY" == "Hardware Port" ]] && PORT="$VALUE";
[[ "$KEY" == "Device" ]] && DEVICE="$VALUE";
if [[ -n "$PORT" && -n "$DEVICE" ]]; then
IP=$(ipconfig getifaddr $DEVICE);
[[ "$IP" != "" ]] && printf "%20s: %s (%s)\n" "$PORT" "$IP" "$DEVICE"; # %20s allows for "Thunderbolt Ethernet"
PORT="";
DEVICE="";
fi;
done < <(networksetup -listallhardwareports);
IP=$(dig +short myip.opendns.com @resolver1.opendns.com);
[[ "$IP" != "" ]] && EXTIP=$IP || EXTIP="inactive";
printf '%20s: %s\n' "External IP" $EXTIP
}