-
-
Save aaronj1335/2975202 to your computer and use it in GitHub Desktop.
cmd line ip checker
This file contains 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
#!/bin/bash | |
# handy script to check your laptop's ip | |
if ! which ip &>/dev/null; then | |
function ip { | |
local addr='\d{1,3}(\.\d{1,3}){3}' | |
# if the argumetn was '-' echo the external ip | |
if [ "$1" = '-' ]; then | |
curl http://checkip.dyndns.org/ 2>/dev/null | \ | |
ack -o '(?<=Address: )'$addr | |
# otherwise if a hostname was given as an argument... | |
elif [ "$1" ]; then | |
# ...echo the corresponding ip if it's in /etc/hosts, otherwise nslookup | |
ack -o $addr'(?=\s*'$1'\s*$)' /etc/hosts || \ | |
nslookup $1 | ack -m 2 '^Address:' | tail -1 | ack -o $addr | |
# otherwise if no argument was given... | |
else | |
# ...echo the this machine's ip of the ethernet or the wireless | |
ifconfig en0 | ack -o '(?<=inet )'$addr || \ | |
ifconfig en1 | ack -o '(?<=inet )'$addr | |
fi | |
} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
check that ip!!
this is a handy way to get your ip from the cmd line
get the ip of your ethernet if it's available, otherwise your wireless
get your external ip (the IP address that websites see when you visit)
get the ip of some host by checking first your
/etc/hosts
file and then DNSwant to snatch it up in your clip board?! no sweat!
note that it's wrapped in a check to only define itself if an
ip
command doesn't currently exist. get fancy and put it in a file on your$PATH
or just copy and paste it into your.bashrc
like a baller.