Until recently, I’d been using WhatIsMyIP.com’s API to fill in the external IP field in my Conky readout, but a few days ago, they started a very silly thing where, for whatever reason, clients have to follow a 301 redirect to the URL they just asked for in order to get the IP. I decided that was excuse enough to write a more direct solution.
Since, recently, I’d learned that it’s possible to muck about in m0n0wall without pulling the CF card (despite the lack of proper SSH), I decided to take m0n0wall’s interfaces readout page, strip out everything that wasn’t necessary to get the WAN IPv4 address, and add an explode() call to separate the IP from the hostmask.
If you want it for your own m0n0wall box, here are the instructions:
- Grab
api_wan_ipaddr4.php
from my GitHub Gist. - Go to exec.php on your m0n0wall box (http://192.168.1.1/exec.php with the default DHCP configuration)
- (optional) Type “
status_interfaces.php
” into the download box and use a tool like diff to verify that I haven’t added anything nefarious. - Use the exec.php upload interface to upload
api_wan_ipaddr4.php
. - Run these commands using the exec field:
mv /tmp/api_wan_ipaddr4.php /usr/local/www chmod 755 api_wan_ipaddr4.php
- Load it in your browser (http://192.168.1.1/api_wan_ipaddr4.php with defaults) to confirm that it’s working.
- Use wget/curl/etc. with HTTP authentication to retrieve your external IP whenever you want. I use this command for my conky:
curl -s --insecure --anyauth --netrc https://192.168.0.1/api_wan_ipaddr4.php
A Quick And Dirty m0n0wall “Get WAN IP” API by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
nice post!
i cant share this link : https://blog.ssokolow.com/archives/2010/07/30/a-quick-and-dirty-m0n0wall-get-wan-ip-api
am i doing it wrong ?
I can’t afford the bump in hosting costs to serve my blog over an encrypted connection. Change the https:// to http:// and it’ll work.
I just tried it with version 1.8.1 and it still works like a charm. Perfect for a script to update my dynamic DNS. Thanks!
I’m glad I could help but it seems odd to me that you can’t use m0n0wall’s built-in dynamic DNS update client.
Does it not support the de facto standard update protocol? Are you already using it for something else? (If so, check out DNS-O-Matic as a way to update multiple services with one DDNS ping.)
Thanks for the follow-up. I’m using freedns.afraid.org for my DDNS, and, as far as I can tell, mono doesn’t support updating it. A bit of Googling didn’t help, but searching for ways to read the external IP got me here.
I see that DNS-O-Matic does support afraid.org, so maybe I should simplify and go that way instead.