Changeset 11950 in genesis for tools/check-batch-cmd
- Timestamp:
- Mar 5, 2013, 6:41:22 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/check-batch-cmd
r11730 r11950 11 11 import os 12 12 import paramiko 13 import socket 14 import struct 13 15 import subprocess 14 import socket15 16 import sys 16 17 import time … … 69 70 else: 70 71 return filter(None, retval)[0] 72 73 74 # http://countergram.com/python-group-iterator-list-function 75 def 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 91 def 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 71 108 72 109 … … 97 134 try: 98 135 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 99 139 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 101 142 except (socket.timeout, socket.error) as e: 102 143 print "### %s (%s)" % (e, addr)
Note:
See TracChangeset
for help on using the changeset viewer.