# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
+import BeautifulSoup
import errno
import math
+import mechanize
import os
import re
import signal
import subprocess
import sys
import time
+import urllib
# configuration
interface = 'eth1'
zclass = 'andersk-auto'
zinstance = '6to4'
zsig = '%s on %s' % (sys.argv[0], socket.gethostname())
+cert_file = '/home/anders/Private/andersk.pem'
# end configuration
mac_re = re.compile(r'^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$')
def msg(m):
os.spawnlp(os.P_WAIT, 'zwrite', 'zwrite', '-q', '-d', '-c', zclass, '-i', zinstance, '-s', zsig, '-m', m)
+br = mechanize.Browser()
+br.add_client_certificate("https://nic.mit.edu", cert_file, cert_file)
+
+def get_owner(mac):
+ for lookup in ('host', 'less'):
+ try:
+ data = urllib.urlencode({
+ 'action': 'Lookup Host',
+ 'lookup': lookup,
+ 'mac': mac
+ })
+ br.open('https://nic.mit.edu/bin/dynareg',
+ data)
+ soup = BeautifulSoup.BeautifulSoup(br.response().read())
+ for tag in soup.findAll('td'):
+ if tag.string == 'owner:':
+ return tag.nextSibling.string or \
+ tag.nextSibling.contents[0]['value']
+ except:
+ pass
+ return None
+
+def show_mac(mac, (t, owner)):
+ return '%s (%s)' % (mac, 'unknown owner' if owner is None else owner)
+
+def show_macs():
+ return ', '.join(show_mac(mac, v) for mac, v in seen_macs.iteritems())
+
def check_gone():
now = time.time()
next = None
- for (mac, t) in seen_macs.items():
+ for (mac, (t, owner)) in seen_macs.items():
if t < now:
del seen_macs[mac]
- msg('Gone 6to4 router: %s\nCurrent 6to4 routers: %s' % (mac, seen_macs.keys()))
+ msg('Gone 6to4 router: %s\nCurrent 6to4 routers: %s' %
+ (show_mac(mac, (t, owner)), show_macs()))
elif next is None or next > t:
next = t
if next is None:
mac = words[1]
t = time.time() + timeout
if mac in seen_macs:
- seen_macs[mac] = t
+ seen_macs[mac][0] = t
else:
- seen_macs[mac] = t
- msg('New 6to4 router: %s\n%s\nCurrent 6to4 routers: %s' % (mac, line.rstrip('\n'), seen_macs.keys()))
+ seen_macs[mac] = [t, get_owner(mac)]
+ msg('New 6to4 router: %s\n%s\nCurrent 6to4 routers: %s' %
+ (show_mac(mac, seen_macs[mac]),
+ line.rstrip('\n'),
+ show_macs()))
else:
print >>sys.stderr, 'Unrecognized line: ', line.rstrip('\n')