Checking your internal and external IP Addresses on a Unix machine
Author: willem In: apple, bash scripting, coding, tips & tricks, tools, unixIf your computer accesses the Internet via a local network connection, it will have an internal (LAN / WiFi) as well as an external (Internet) IP Address. If you need to reference either of these on a Unix machine, you can do so relatively easily from the command line.
Checking your internal IP address:
To check the IP Address of your LAN / WiFi connection, ensure that Perl is installed, then run this command on the command line:
ifconfig | perl -nle '/inet [addr:]*(\S+)/ && print $1'
Explanation:
“ifconfig” will output your computer’s network interface parameters. These results will be piped into a Perl script with “perl -nle” running the specified line of code with line ending processing enabled and assuming a “while (<>) { … }” loop around the code. The “/inet [addr:]*(\S+)/ && print $1” line of code will output the IP Address section of the result of a regular expression search for lines containing “inet ” followed by an optional “addr:“.
Checking your external IP address:
To check the address of your Internet connection, ensure that cURL is installed, then run this command on the command line:
curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z/<> :]//g'
Explanation:
“curl -s http://checkip.dyndns.org” will fetch the source of the specified URL without displaying a progress meter or error messages. This result will be piped into sed, and “sed ’s/[a-zA-Z/<> :]//g’” will replace all alphabetic characters, forward slashes, lesser than, greater than, space and colon characters with ” (nothing) globally (throughout the entire input, not just up to the first instance).
For more information on perl, curl and sed, use these commands:
man perl man curl man sed perl --help curl --help sed --help
Related posts:
- Checking which processes are accessing the Internet on a Unix machine
- Testing SMTP servers from a UNIX command line
- UNIX Quick Tip: See the current UNIX Timestamp and other date formats on the command line
- A Python script to check Google rankings for a specific domain and search term
- Checking directory sizes on a Bash command line
Like this post? Subscribe to the Geekology RSS 2.0 feed!













Leave a reply