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

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

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

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

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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.