- Timestamp:
- Apr 12, 2012, 7:15:42 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/check-batch-cmd
r10102 r10433 9 9 import gformat 10 10 import getpass 11 import netsnmp12 11 import os 13 12 import paramiko … … 17 16 18 17 SSHPASS = None 19 netsnmp.verbose = 0 18 import pysnmp 19 from pysnmp.entity.rfc3413.oneliner import cmdgen 20 21 def snmp_test(): 22 errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd( 23 # SNMP v1 24 # cmdgen.CommunityData('test-agent', 'public', 0), 25 # SNMP v2 26 cmdgen.CommunityData('test-agent', 'public'), 27 # SNMP v3 28 # cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'), 29 cmdgen.UdpTransportTarget(('localhost', 161)), 30 # Plain OID 31 (1,3,6,1,2,1,1,1,0), 32 # ((mib-name, mib-symbol), instance-id) 33 (('SNMPv2-MIB', 'sysObjectID'), 0) 34 ) 35 36 if errorIndication: 37 print errorIndication 38 else: 39 if errorStatus: 40 print '%s at %s\n' % ( 41 errorStatus.prettyPrint(), 42 errorIndex and varBinds[int(errorIndex)-1] or '?' 43 ) 44 else: 45 for name, val in varBinds: 46 print '%s = %s' % (name.prettyPrint(), val.prettyPrint()) 47 48 20 49 21 50 class CmdError(Exception): 22 51 pass 23 52 24 def check_host(hostname): 25 cmd = "cat /etc/board.info" 26 53 54 55 def host_ssh_cmd(hostname, cmd): 27 56 ssh = paramiko.SSHClient() 28 57 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) … … 32 61 stderr = stderr.readlines() 33 62 ssh.close() 34 if stderr: 35 raise CmdError(stderr) 36 37 return dict(map(lambda x: x.strip().split('='),stdout)) 63 if stderr: 64 raise CmdError((stderr, stdout)) 65 return stdout 66 67 def parse_ini(lines): 68 return dict(map(lambda x: x.strip().split('='),lines)) 69 70 def ubnt_probe(hostname): 71 items = parse_ini(host_ssh_cmd(hostname, 'cat /etc/board.info')) 72 print items 73 38 74 39 75 def get_bridge_type(host): … … 51 87 52 88 53 def update_hosts(filters=[]): 54 for host in gformat.get_hostlist(): 55 if filters and not any([f.lower() in host.lower() for f in filters]): 56 continue 57 58 print "# Processing host", host 59 datadump = gformat.get_yaml(host) 60 for iface_key in datadump['autogen_iface_keys']: 61 ifacedump = datadump[iface_key] 62 if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']: 63 addr = ifacedump['ns_ip'].split('/')[0] 64 print "## Bridge IP: %(ns_ip)s at %(interface)s" % ifacedump 65 try: 66 socket.create_connection((addr,80),2) 67 bridge_type = get_bridge_type(addr) 68 datadump[iface_key]['bridge_type'] = bridge_type 69 except (socket.timeout, socket.error) as e: 70 print "### %s (%s)" % (e, addr) 71 except paramiko.AuthenticationException: 72 print "### Conection failed (invalid username/password)" 73 except CmdError, e: 74 print "### Command error: %s" % e 75 gformat.store_yaml(datadump) 76 89 def node_check(host): 90 print "# Processing host", host 91 datadump = gformat.get_yaml(host) 92 output = host_ssh_cmd(datadump['autogen_fqdn'], 'cat /var/run/dmesg.boot') 93 94 # Get board Type 95 for line in [x.strip() for x in output]: 96 if line.startswith('CPU:'): 97 print line 98 elif line.startswith('Geode LX:'): 99 datadump['board'] = 'ALIX2' 100 print line 101 elif line.startswith('real memory'): 102 print line 103 elif line.startswith('Elan-mmcr'): 104 datadump['board'] = 'net45xx' 105 #for iface_key in datadump['autogen_iface_keys']: 106 # ifacedump = datadump[iface_key] 107 # if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']: 108 # addr = ifacedump['ns_ip'].split('/')[0] 109 # print "## Bridge IP: %(ns_ip)s at %(interface)s" % ifacedump 110 # try: 111 # socket.create_connection((addr,80),2) 112 # bridge_type = get_bridge_type(addr) 113 # datadump[iface_key]['bridge_type'] = bridge_type 114 # except (socket.timeout, socket.error) as e: 115 # print "### %s (%s)" % (e, addr) 116 # except paramiko.AuthenticationException: 117 # print "### Conection failed (invalid username/password)" 118 # except CmdError, e: 119 # print "### Command error: %s" % e 120 gformat.store_yaml(datadump) 121 77 122 78 123 def make_output(stdout, stderr): … … 125 170 subparsers = parser.add_subparsers(help='sub-command help') 126 171 127 parser_snmp = subparsers.add_parser('snmp', help='enable SNMP on UBNT') 172 parser_snmp = subparsers.add_parser('bridge', help='UBNT Bridge Management') 173 parser_snmp.add_argument('action', type=str, choices=['keys', 'snmp', 'probe']) 128 174 parser_snmp.add_argument('host',type=str) 129 parser_snmp.set_defaults(func='snmp') 130 131 parser_keys = subparsers.add_parser('keys', help='add ssh keys on UBNT') 132 parser_keys.add_argument('host', type=str) 133 parser_keys.set_defaults(func='keys') 134 135 parser_update = subparsers.add_parser('update', help='process all UBNT') 136 parser_update.add_argument('filters', default=None, nargs='*', type=str) 137 parser_update.set_defaults(func='update') 138 175 parser_snmp.set_defaults(func='bridge') 176 177 parser_node = subparsers.add_parser('node', help='Proxy/Node/Hybrid Management') 178 parser_node.add_argument('action', type=str, choices=['check',]) 179 parser_node.add_argument('host', type=str) 180 parser_node.set_defaults(func='node') 181 139 182 args = parser.parse_args() 140 183 … … 146 189 SSHPASS = getpass.getpass("WL root password: ") 147 190 148 149 if args.func == 'keys': 150 ubnt_keys(args.host) 151 elif args.func == 'snmp': 152 ubnt_snmp(args.host) 153 elif args.func == 'update': 154 update_hosts(args.filters) 191 # XXX: We need this loop when using filters 192 #for host in gformat.get_hostlist(): 193 # if filters and not any([f.lower() in host.lower() for f in filters]): 194 # continue 195 196 197 if args.func == 'bridge': 198 if args.action == 'keys': 199 ubnt_keys(args.host) 200 elif args.action == 'snmp': 201 ubnt_snmp(args.host) 202 elif args.action == 'probe': 203 ubnt_probe(args.host) 204 elif args.func == 'node': 205 if args.action == 'check': 206 node_check(args.host)
Note:
See TracChangeset
for help on using the changeset viewer.