Getting the External IP Address of your Computer and Storing it in a File

My computer is one of three computer on our LAN. Our router is a Time Capsule. My computer is the only of of three computers that has been assigned a static IP address on the LAN, because it runs an Apache installation. I tinker around with web development from time to time, and I wanted to be able to demo my experiments to my friends without having to throw it on a flash drive.

My home computer doesn't have a domain name, of course. I access it with my IP address, which is dynamic, and changes whenever the cats dislodge the modem.

I'm trying to write a script that checks my external IP (there's only one, we don't have aliases or multiple routers or anything), and compares to the IP in a saved file. If it finds that the IP address it detected is different from the one in the file, it sends me an email and saves the new IP to the file, where it check it later on.

  1. The script is called by launchd every 5 minutes.
  2. The script checks my external IP.
  3. It compares it to the IP in its save file.
  4. If the IP is the same as the IP in the save file, the script quits.
  5. If the IP is different, the script activates another program that sends me an email.
  6. I recieve the email and can refer to IP address by looking at the message's header.

I'm new to UNIX, but not totally clueless. I can move around in my computer, move, copy, and delete files, ping servers, and other basic tasks. I usually learn a new command on my own, but in this case, ifconfig (which I think I need) is just way out of my league. Can anyone help?

ifconfig won't be of any help in this case... your external ip is assigned to your router. you have to read the ip from your router (if this is possible).
why don't you use a ddns service like DynDNS: DNS Hosting, Email Delivery and Other Services ?

I didn't know such a service existed. Thanks for the reference!

Note that most routers nowadays should be able to tell you the WAN IP via UPNP.

I wrote a script for someone to output the network information of a client machine to a text file. It doesn't display the external IP, but I am sure it can be tweaked to do so.

#!/bin/sh

/usr/sbin/networksetup -getinfo Ethernet | open -t -a

If running the command manually you need sudo, but DYNDNS.org is probably a better solution.

You can also get your external ip address with curl.

curl -L -s --max-time 10 http://checkip.dyndns.org | egrep -o -m 1 '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'   
1 Like

why not just download and install the dyndns client from DynDNS: DNS Hosting, Email Delivery and Other Services which will track your external IP for you?

look at bwanadik if you want to be emailed when your wan ip changes.

or use Dynamic DNS.

or buy a static IP from your ISP around �5 p/m or free with many isps so go check quick.