Get the internet IP address from your Airport via bash script

I figured I’d post this up in case anyone else has an Apple Airport Extreme, and wants to get the internet IP address from it without opening the AirPort Utility. I slapped a script into place that uses snmpwalk to get the IP address, and spits it out as stdout. What I do is pipe it into a file and SCP it to my webpage so I can snag it whenever I want. (I port map port 22 to my Linux machine)
First thing to do is open the AirPort Utility.
I’ve only been able to turn SNMP on with AirPort Utility 5.6. If you need a link to it, here you go:
Open the AirPort Utility, and select the “manual setup” button at the bottom. Then, select “Advanced” at the top and in the “Logging & Statistics” tab halfway down the screen you will see a radio button labeled, “Allow SNMP”. Highlight that, and change the SNMP community string underneath if you desire. (and remember it temporarily) Click the “Update” button at the bottom right, then you’re safe to close the utility.

Here’s the script. Change the SMNPKEY variable to reflect your community string, if you changed it above:

if [ $# = 1 ] ; then
IFS=” ”
set — $(netstat -nr | egrep ‘^|^default’)
set — $(snmpwalk -Os -c “$SNMPKEY” -v 1 “$ROUTER” ipRouteNextHop.
set — $(snmpwalk -Os -c “$SNMPKEY” -v 1 “$ROUTER” ipRouteNextHop.$4)
echo $4

That’s it. Pop that into a script, throw it onto your Unix box (OS/X, Linux, whatever) and you’re set.
I have it in cron, ran every 10 minutes because I’m crazy like that, and piping it to a file in the /tmp directory. I then have another cron that scp’s the file to a subdirectory on another server that houses my website. All I have to do is go to that file through http and I can snag the IP.
My crontab entries (sanitized of personal information, be sure to change userid to your userid, to your server’s hostname or IP, & edit the directory where the file will go.)
*/10 * * * * /u01/scripts/ > /tmp/ip.address
*/10 * * * * scp /tmp/ip.address 2>&1 > /dev/null


Edit September 20, 2013

I haven’t used this script in quite some time since it was a one time thing for me, and I posted/forgot about it.  However, thanks to a commenter named Ali, I revisited the script and made it work with Leopard.  The reason it is only running on Leopard is because my Mac Mini PowerPC (yes, not Intel, PowerPC) is my utility box sitting underneath my monitor.  My Airport Extreme was configured a while back to only be SNMP accessible from that machine, so my desktop nor laptop can access it.  Unfortunately also, SNMP functionality support has been discontinued by Apple on the Airport Extreme.  The Airport Utility 6 has been missing the configuration feature for quite some time, and it appears Airport Utility 5.6 does not work with Mountain Lion.

At any rate, if you’re like I am and have an Airport Extreme configured for SNMP this should work for you.  Pass the SNMP key as the parameter if you have something other than ‘public’ (the default).  You can also just change “public” to whatever your SNMP key is.  Here it is:

if [ $# = 1 ] ; then
ROUTER=`netstat -nr | grep ^default | awk ‘{ print $2 }’`
ROUTE=`snmpwalk -Os -c “$SNMPKEY” -v 1 “$ROUTER” ipRouteNextHop. | awk ‘{ print $4 }’`
EXTERNAL=`snmpwalk -Os -c “$SNMPKEY” -v 1 “$ROUTER” ipRouteNextHop.${ROUTE} | awk ‘{ print $4 }’`


2 thoughts on “Get the internet IP address from your Airport via bash script

  1. Hey nice work. It’s not working on my mac (10.8). I’m trying to retrieve the ip address of my airport express on my LAN. However, when I run this script it gives me this error: /Users/MN01/Desktop/ line 6: “: command not found
    /Users/MN01/Desktop/ line 7: ^default’: command not found
    No log handling enabled – using stderr logging
    ipRouteNextHop. Unknown Object Identifier (Sub-id not found: (top) -> ipRouteNextHop)
    No log handling enabled – using stderr logging
    ipRouteNextHop.: Unknown Object Identifier (Sub-id not found: (top) -> ipRouteNextHop)

    Do I need to change something else in the script?

  2. Ali,
    I’ve updated the post to reflect changes that I’ve made to the script. It looks like things changed behind the scenes. I don’t use the script much if at all since it was a one-off for me, however I’ve updated the script and added the changed on to the post.
    It looks like Apple is not supporting SNMP any longer on the Airport Extreme, which really upsets me. If you have Airport Utility 5.6 you should be good, but unfortunately it doesn’t work with Mountain Lion or higher.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.