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 192.168.1.1 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 192.168.1.1 ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 3
IP-MIB::ipAdEntIfIndex.127.0.0.2 = INTEGER: 3
IP-MIB::ipAdEntIfIndex.169.254.38.226 = INTEGER: 5
IP-MIB::ipAdEntIfIndex.192.168.18.1 = INTEGER: 6
IP-MIB::ipAdEntIfIndex.65.65.65.65 = INTEGER: 5

The one I marked 65.65.65.65 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!

Advertisements

GeekTalkerTom

This is the first post for geektalker. The reason I am starting this blog is because I want a place to document all the geek things I do. Everyone is always asking me how to setup this or do that. So this site is a place where I can tell them.

I have a natural tendency to over explain things and get far down the rabbit whole. I will try to cut-back the words and do as many examples I can. I plan to put tutorials up, reviews on products I am using, and many other geek related things.

Please feel free to send suggestions for stories you might be interested or if you want me do some review on a product/products. I will also post reviews and reactions to very “geeky” things like movie reviews for Science Fiction / Action movies. Or what ever pops in my brain…

I will sign of with a quote, what movie is it? 🙂

“My Brain Hurts?!?”

-GeekTalkerTom