Skip to content

Instantly share code, notes, and snippets.

@rmanly
Created July 25, 2018 15:50
Show Gist options
  • Save rmanly/a0ca0a7e35f8aad33049460b2434120c to your computer and use it in GitHub Desktop.
Save rmanly/a0ca0a7e35f8aad33049460b2434120c to your computer and use it in GitHub Desktop.
Prints the first IP address encountered based on network service order
#!/bin/bash
devices=($(networksetup -listnetworkserviceorder | awk -F': ' '/Port/{ gsub(/\)$/,""); print $3 }'))
for iface in "${devices[@]}"; do
if ! ipconfig getifaddr "$iface"; then
continue
else
exit 0
fi
done
echo "None"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment