Get your IP address

G

gatorparrots

Guest
If you are in a DHCP situation and find your IP address frequently changing, here is an easy way to determine it:
ipconfig getifaddr en0
and an alternate (albeit more convoluted method):
ifconfig en0 inet | grep 'inet ' | awk ' { print $2 } '

If you are behind a router and need your external IP address, issue this command:
wget -q -O /dev/stdout http://checkip.dyndns.org/ | grep 'Current IP Address: ' | awk ' { print $4 } '
(Of course, you would have to have wget installed in order for this to work.)

Here is a shell script for checking your IP address and logging the results to a file:
Code:
#!/bin/sh
#initialize variable
IP=`wget -q -O /dev/stdout <a href='http://checkip.dyndns.org/' target='_blank'>http://checkip.dyndns.org/</a> | grep 'Current IP Address: ' | awk ' { print $4 } '`
# prints current IP to shell
echo $IP
#log it
date >> /private/var/log/ip.log
echo $IP >> /private/var/log/ip.log
This script could be added to your crontab in order to check your IP address periodically and log the results to a file.
 
Joined
Feb 25, 2003
Messages
5,279
Reaction score
138
Points
63
Location
Tropical Island, Jealous?
Your Mac's Specs
MacPro 3.0Ghz 16GB RAM, 4x256 Vid, 30''cinema display
this thread is over a year old... i almost forgot gatorparrots existed.. theres a few otherways, if you need to find it, PM and ill show u a lil thing i came across, ide post it but it may scare a few people.
 
OP
Z

zoe77

Guest
thanks Graphite... i'm behind a router so the wget method would suit me best.
 
OP
G

gatorparrots

Guest
Lately, I've been using this code to obtain my external IP address:
Code:
curl -s [url]http://www.showmyip.com/simple/[/url] | awk '{print $1}'

However, if you have a Linksys BEFSR-series router, the following code is better for scripting purposes, as it obtains results in about half the time:
Code:
/usr/bin/curl --connect-timeout 5  -s \
           http://:$PASSWORD@$ROUTERIP/Status.htm  | \
           sed -e 's/^.*IP Address://'              | \
           sed -e 's/=2>/\\
/'                                                  | \
           sed -e '1 d'                             | \
           sed -e 's/<.*//'
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top