Skip to content

Instantly share code, notes, and snippets.

@stoph
Created September 8, 2025 02:10
Show Gist options
  • Select an option

  • Save stoph/e3f7dfd48f580796ea9a2f81cb105761 to your computer and use it in GitHub Desktop.

Select an option

Save stoph/e3f7dfd48f580796ea9a2f81cb105761 to your computer and use it in GitHub Desktop.
zsh function to show local ip address (~/.zshrc)
ip() {
local ifc addr
ifc=$(route -n get default 2>/dev/null | awk '/interface:/{print $2}')
if [[ -n "$ifc" ]]; then
addr=$(ipconfig getifaddr "$ifc" 2>/dev/null)
if [[ "$addr" =~ ^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.) ]]; then
echo "$addr"
return
fi
fi
for ifc in en0 en1 en2 en3; do
addr=$(ipconfig getifaddr "$ifc" 2>/dev/null)
if [[ "$addr" =~ ^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.) ]]; then
echo "$addr"
return
fi
done
echo "no LAN IPv4" >&2
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment