A Quick And Dirty m0n0wall “Get WAN IP” API

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:

  1. Grab api_wan_ipaddr4.php from my GitHub Gist.
  2. Go to exec.php on your m0n0wall box (http://192.168.1.1/exec.php with the default DHCP configuration)
  3. (optional) Type “status_interfaces.php” into the download box and use a tool like diff to verify that I haven’t added anything nefarious.
  4. Use the exec.php upload interface to upload api_wan_ipaddr4.php.
  5. Run these commands using the exec field:
    mv /tmp/api_wan_ipaddr4.php /usr/local/www
    chmod 755 api_wan_ipaddr4.php
  6. Load it in your browser (http://192.168.1.1/api_wan_ipaddr4.php with defaults) to confirm that it’s working.
  7. 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

CC BY-SA 4.0 A Quick And Dirty m0n0wall “Get WAN IP” API by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

5 Responses to A Quick And Dirty m0n0wall “Get WAN IP” API

  1. Arturo says:

    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.)

      • Arturo says:

        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.

Leave a Reply to hundertwasser Cancel reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.