Wednesday, July 31, 2013

Easily get your external IP address using the terminal or command line

I want my little Raspberry Pi to "phone home" once in a while so I know where it's at. I wanted a very simple web service that simply regurgitated the requester's IP address in plain text. Nothing more, nothing less.

About to write it myself, I typed in and found exactly what I wanted. It's been a good day!

To get your external IP address from the command line or Linux terminal:

$ curl

To save it to a file:

$ curl -s > myip

Or, if you prefer wget:

$ wget --no-cache -qO-

To get super-nerdy and make your life even easier:

$ alias echoip="curl"
$ echoip

(You could use any of the variations above for your alias' command.)

In the end, this is the command I added to my Raspberry Pi's crontab (I've changed sensitive parts):

$ curl -s | ssh "cat > /www/mysite/ipaddress.txt"

As a side note, if you want JSON output, there's, or for XML, there's (which actually does XML, JSON, and plain-text, but I feel like the server is a bit slower -- and the homepage doesn't actually serve XML. *facepalm*)

I don't know who made, but props to them for choosing the same name I would have. (For some reason, I thought that the output at would be... well, plainer.) Memo to self: if echoip ever goes offline, write my own to replace it.