Mac OSX one-liners to get specific IP Info

I needed to allow users to print very specific networking information with ease.
A typical user doesn’t want to decipher all of the extra info and steps an ifconfig or similar command can bring.
While building this script, I ended up with quite a few one-liners that could be useful for every day admin tasks

Print ONLY IP Address
ipconfig getifaddr en0

print ONLY Gateway
route -n get default | awk '/gateway: / {print $2;} '

print ONLY Network Mask for Wi-Fi connection
networksetup -getinfo "Wi-Fi" | awk '/Subnet mask:/ {print $3;} '

print ONLY Network Address
ip=$(ipconfig getifaddr en0) && nm=$(networksetup -getinfo "Wi-Fi" | awk '/Subnet mask:/ {print $3;} ') && IFS=. read -r i1 i2 i3 i4 <<< "$ip" && IFS=. read -r m1 m2 m3 m4 <<< "$nm" && printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$(($i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"

Print all DNS servers on one line
cat /etc/resolv.conf | awk '/nameserver / {print $2;} ' | tr "\n" " "

Leave a Reply

Your email address will not be published.