External Ip Address using python, snmpwalk, and AirPort Extreme

What you need?
– AirPort or AirPort extreme with snmp enabled
– Some Python knowledge (please don’t post questions about python, that’s for the python organization to help with)

1) Open AirPort Utility (/Applications/Utilities/AirPort Utility.app)
2) Click your AirPort or AirPort Extreme Device (ignore or cancel any automatic setups)
3) Click “Manual Setup” Lower left button
4) Click “Advanced”
5) Click “Logging and Statistics”
6) Check “Allow SNMP”

AirPort Extreme Configuration

Allow SNMP

NOTE: You can also change the SNMP Community String, for security I highly recommend this unless your AirPort is behind a hardware or server firewall.
If you change the string your Python Script will need to be updated.

The Python Script:

import os
import sys
import subprocess

CMD = 'snmpwalk -v 2c -c public ipAdEntIfIndex'
EXCLUDE_LIST = ['127', '192', '169', '10'] # exclude private ips

def get_external_ip():
 PIPE = subprocess.PIPE
 r = subprocess.Popen(CMD, shell=True, stdout=PIPE, stderr=PIPE)
 result = r.communicate()[0]
#    import pdb; pdb.set_trace()
 for ip in result.split('\n'):
 if '=' in ip:
 parts = ip.split('=')[0]
 ipaddress = ".".join(parts.split('.')[1:])
 first_part = parts.split('.')[1]
 if first_part not in EXCLUDE_LIST:
 return ipaddress

if __name__=='__main__':
 print get_external_ip()

Once you have your external ip address printing to the shell. You can now do things like create a cron job on your server to push a text file to a public or private webserver for example. I recommend it be pushed to something you need a username and password to get in from the outside world. Maybe an ftp address that you can only access as admin.

Checking your command line:

snmpwalk -v 2c -c public ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex. = INTEGER: 3
IP-MIB::ipAdEntIfIndex. = INTEGER: 3
IP-MIB::ipAdEntIfIndex. = INTEGER: 5
IP-MIB::ipAdEntIfIndex. = INTEGER: 6
IP-MIB::ipAdEntIfIndex. = INTEGER: 5

The one I marked would be your address most-likely. Remember, if you cant get this command to work the python script above is useless. So what ever you get working here you should put in the CMD =”” string in your python script. We skip over the 127/169/192/10 in our python script because these are private address. When we do this the remainder is your external ip address.

I could not find an easy way just yet using snmp protocol to get the external ip so this is the next best thing.

Please give it a try and post your comments!


Leave a Reply

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

WordPress.com Logo

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

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s

%d bloggers like this: