Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aaronj1335
Created June 22, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronj1335/2975202 to your computer and use it in GitHub Desktop.
Save aaronj1335/2975202 to your computer and use it in GitHub Desktop.
cmd line ip checker
#!/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
@aaronj1335
Copy link
Author

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

$ ip

get your external ip (the IP address that websites see when you visit)

$ ip -

get the ip of some host by checking first your /etc/hosts file and then DNS

$ ip hostname.example.com

want to snatch it up in your clip board?! no sweat!

$ ip | pbcopy        # or something like clip on linux

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment