Know Your External IP

My name is Bruce and I'm a UNIX geek. There, I've said it. Maybe it's my start to a 10 step program but I hope not. I like my addiction. The thing about being a UNIX geek is it's fun to figure out how to do something behind the scenes using terminal or your favorite text editor. Somethings you look for a problem you don't really have just to try to fix it using the command line. For me I wanted to find a way to identify my external IP address so I could access my computer remotely.

If you are away from your computer and want to access it how do you do it? Apple has built in Back-to-My-Mac or Screen sharing. There are also tons of  Mac VNC apps (Chicken of the VNC, JollyFastVNC, TeamViewer, etc) or iOS connection apps (VNC Viewer, Remotix, LogMeIn, etc.).  Whatever the interface or OS they all have one thing in common, they need to know the IP address of the target machine. Without that you don't know where you're going.

Some of the VNC apps, most that charge you money, will configure your target Mac with a small background app that registers with their server and records the IP address so you don't need to worry about it. You can also use a service like Dyndns to register your machine for free but they have changed their set up so your hostname will expire every 30 days unless you upgrade to the pro version (which costs money). So what do you do if you're cheap and don't want someone else to keep track of the address? How do you do it yourself and make sure you know an address that can change at any moment? This is where the fun with UNIX begins.

This example assumes you have a free cloud service like dropbox. You need a place to record your IP address that's available to you when you are away from home. You need to be able to update that file on a regular basis so if your IP changes you can record it. That means you need a UNIX script to run at regular intervals and record your IP address. To do that you will use a UNIX application called cron.

Every flavor of UNIX, including the BSD core that Mac OS uses, has cron. It allows you to schedule jobs using to run when you want. Any time of day. Any day of the week, month of the year and anything in between. The data for cron is stored in the crontab file and by editing this file you can set a script to run. Each user has a crontab file that cron looks at when you log in. To see your own file type the following at the command line.

crontab -l return

You will either get a statement that your crontab file is empty or it will show you what's currently in your crontab file. If you are like most people you will just see a header line starting with the pound symbol. 

To set up our IP address recording script you will need to edit this file. UNIX is very picky about how things are set up and edited so you will need to use a command line editor like EMACS or VI. Sounds scary but I'll give the exact keystrokes to make it work using the VI editor.  Make sure to type EXACTLY what is written! I will use bold in red for hitting the return key or other command keys. 

crontab -e return

You should see something like this on the screen

Now just type the letter "o" without hitting the return key. That's the  "oh" key not zero. Again, Don't hit return. The "o" is short for "open". Now your cursor should be on the next line down.

Now is a good time to tell you how to get out of things if you mistype or screw up. At any time you can hit the "esc" key ( the one in the upper left corner of your keyboard) and type 

esc :q! return

This says to escape to the VI command line and quit without saving. You loose your edits but your file doesn't get hosed up.

So... back to editing the file. After you have done the "crontab -e" command and opened a new line you can type the following. Make sure to replace "UserID" with your own Mac userID. Obviously, if you have placed the default dropbox folder somewhere else you would enter that path instead.

0 0,12 * * *  date | awk '{print $2" "$3" "$4}' > /users/UserID/Dropbox/IP.txt;curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z/<> :]//g' >> /users/UserID/Dropbox/IP.txt

This command is all on one line with no returns. Also, a special character worth noting is the pipe  "|" character. It is located just below the delete key, above the return key. The pipe symbols says take the output of one part of the script and use it as the input of the next part.

I won't parse the whole line for you but basically the cron will run mini-script at noon and midnight every day of the week.  The script will record the date and time and write it on the first line of your text file (IP.txt) that is located in your dropbox folder. Then run a check using "checkip.dyndns.org" to find your external IP. It takes what the website returns, gets rid of the garbage and only keep the IP address and writes that IP to the IP.txt file.

Once that's done with your typing you need to save your file.

esc :wq return

That command writes the file and quits. Once you are back at the main command line you can check your work by retyping the crontab list command.

crontab -l return

You should now see the file with your added line.

Another command worth noting. If you mess up and save the file with a typo or other error, you can clear out the file using the following command at the UNIX prompt and start all over.

crontab -r return

But if you are careful you shouldn't need to use that command. If everything goes well you should now have a file in your dropbox folder that you can check to find out the external IP address of your computer.

Now for a shameful admission. Do you remember when I said sometimes you look for a problem you don't really have just to try to fix it using the command line?  Well, just so you know, if you have dropbox you already have access to your external IP address.

Open any web browser you can find out the external address of any of your currently connected devices. Go to the dropbox web page and open up your settings.

Choose the "Security" tab and you will see a list of all your devices. Hover your mouse over the information icon for any of your devices.

Guess what?  You now know your external IP without using any UNIX at all and it tells you the last time it was updated. But where's the fun in that?

1 comment:

  1. Nice information.I had checked my External ip address from the site IP-Details.com