Topic: https://brettterpstra.com/2017/10/30/a-few-new-shell-tricks/
hide preview

What's next? verify your email address for reply notifications!

Keith Rollin 7y, 139d ago

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
...

remark link
hide preview

What's next? verify your email address for reply notifications!

ttscoff 7y, 139d ago

Thanks to both of you. I'll be updating the script shortly!

hide preview

What's next? verify your email address for reply notifications!

Daniel Whicker 7y, 139d ago

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
}

remark link parent
hide preview

What's next? verify your email address for reply notifications!

Keith Rollin 7y, 138d ago

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
}

hide preview

What's next? verify your email address for reply notifications!