Changeset 11950 in genesis for tools/check-batch-cmd


Ignore:
Timestamp:
Mar 5, 2013, 6:41:22 AM (12 years ago)
Author:
rick
Message:

Automatic Nanostation MAC discovery to be put into genesis.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/check-batch-cmd

    r11730 r11950  
    1111import os
    1212import paramiko
     13import socket
     14import struct
    1315import subprocess
    14 import socket
    1516import sys
    1617import time
     
    6970  else:
    7071    return filter(None, retval)[0]
     72
     73
     74# http://countergram.com/python-group-iterator-list-function
     75def group_iter(iterator, n=2, strict=False):
     76    """ Transforms a sequence of values into a sequence of n-tuples.
     77    e.g. [1, 2, 3, 4, ...] => [(1, 2), (3, 4), ...] (when n == 2)
     78    If strict, then it will raise ValueError if there is a group of fewer
     79    than n items at the end of the sequence. """
     80    accumulator = []
     81    for item in iterator:
     82        accumulator.append(item)
     83        if len(accumulator) == n: # tested as fast as separate counter
     84            yield tuple(accumulator)
     85            accumulator = [] # tested faster than accumulator[:] = []
     86            # and tested as fast as re-using one list object
     87    if strict and len(accumulator) != 0:
     88        raise ValueError("Leftover values")
     89
     90
     91def get_bridge_mac(host):
     92  """ Both NS and NS Mx uses a slighly different OID"""
     93  var_list = netsnmp.VarList(
     94   *map(lambda x: netsnmp.Varbind(x),
     95    ['IF-MIB::ifDescr','IF-MIB::ifPhysAddress']))
     96  sess = netsnmp.Session(Version=1, DestHost=host, Community='public', Timeout=6 * 100000, Retries=1)
     97  retval = sess.walk(var_list)
     98  if sess.ErrorInd < 0:
     99    raise CmdError('SNMP Failed -- [%(ErrorInd)s] %(ErrorStr)s (%(DestHost)s)' % vars(sess))
     100  if not filter(None, retval):
     101    return None
     102  else:
     103    # We only have bridge configurations, so looking at bridge MAC addresses
     104    mac_raw = dict(group_iter(retval,2))['br0']
     105    return ':'.join(map(lambda x: "%02x" % x,struct.unpack("BBBBBB",mac_raw)))
     106
     107
    71108
    72109
     
    97134      try:
    98135        socket.create_connection((addr,80),2)
     136        bridge_mac = get_bridge_mac(addr)
     137        if bridge_mac:
     138          datadump[iface_key]['ns_mac'] = bridge_mac
    99139        bridge_type = get_bridge_type(addr)
    100         datadump[iface_key]['bridge_type'] = bridge_type
     140        if bridge_type:
     141          datadump[iface_key]['bridge_type'] = bridge_type
    101142      except (socket.timeout, socket.error) as e:
    102143        print "### %s (%s)" % (e, addr)
Note: See TracChangeset for help on using the changeset viewer.