source: genesis/tools/check-batch-cmd@ 10092

Last change on this file since 10092 was 10092, checked in by rick, 13 years ago

Better output processing and variable checking.

  • Property svn:executable set to *
File size: 2.2 KB
RevLine 
[10074]1#!/usr/bin/env python
2# vim:ts=2:et:sw=2:ai
3#
4# Check configs with remote addresses
5#
6# Rick van der Zwet <info@rickvanderzwet.nl>
7#
8import gformat
[10081]9import netsnmp
10import paramiko
11import socket
[10074]12import sys
13
[10081]14netsnmp.verbose = 0
15
[10074]16class CmdError(Exception):
17 pass
18
19def check_host(hostname):
20 cmd = "cat /etc/board.info"
21
22 ssh = paramiko.SSHClient()
23 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
[10075]24 ssh.connect(hostname, username='root', password='XXXXXXXXXX',timeout=3)
[10074]25 stdin, stdout, stderr = ssh.exec_command(cmd)
26 stdout = stdout.readlines()
27 stderr = stderr.readlines()
28 ssh.close()
29 if stderr:
30 raise CmdError(stderr)
31
32 return dict(map(lambda x: x.strip().split('='),stdout))
33
[10075]34def get_bridge_type(host):
[10081]35 """ Both NS and NS Mx uses a slighly different OID"""
36 var_list = netsnmp.VarList(
37 *map(lambda x: netsnmp.Varbind(x),
38 ['.1.2.840.10036.3.1.2.1.3.6', '.1.2.840.10036.3.1.2.1.3.7']))
[10075]39
40 sess = netsnmp.Session(Version=1, DestHost=host, Community='public', Timeout=2 * 100000, Retries=0)
41 retval = sess.get(var_list)
42 if sess.ErrorInd < 0:
[10092]43 raise CmdError('[%(ErrorInd)s] %(ErrorStr)s (%(DestHost)s)' % vars(sess))
[10081]44 return filter(None, retval)[0]
[10074]45
[10075]46
[10082]47
[10092]48def main(hostfilter=None):
[10074]49 for host in gformat.get_hostlist():
[10092]50 if hostfilter and not hostfilter in host:
51 continue
[10074]52 print "# Processing host", host
53 datadump = gformat.get_yaml(host)
54 for iface_key in datadump['autogen_iface_keys']:
55 ifacedump = datadump[iface_key]
[10092]56 if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']:
[10074]57 addr = ifacedump['ns_ip'].split('/')[0]
58 print "## Bridge IP: %s" % addr
59 try:
60 socket.create_connection((addr,80),2)
[10075]61 bridge_type = get_bridge_type(addr)
62 datadump[iface_key]['bridge_type'] = bridge_type
[10092]63 except (socket.timeout, socket.error) as e:
64 print "### %s (%s)" % (e, addr)
[10074]65 except paramiko.AuthenticationException:
66 print "### Conection failed (invalid username/password)"
67 except CmdError, e:
68 print "### Command error: %s" % e
69 gformat.store_yaml(datadump)
70
71
72if __name__ == '__main__':
[10092]73 if len(sys.argv) == 2:
74 main(hostfilter=sys.argv[1])
[10081]75 else:
76 main()
[10083]77
[10084]78
[10085]79
[10086]80
[10087]81
[10088]82
[10089]83
Note: See TracBrowser for help on using the repository browser.