Created
September 8, 2025 02:10
-
-
Save stoph/e3f7dfd48f580796ea9a2f81cb105761 to your computer and use it in GitHub Desktop.
zsh function to show local ip address (~/.zshrc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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