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
Line 
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
9import netsnmp
10import paramiko
11import socket
12import sys
13
14netsnmp.verbose = 0
15
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())
24 ssh.connect(hostname, username='root', password='XXXXXXXXXX',timeout=3)
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
34def get_bridge_type(host):
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']))
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:
43 raise CmdError('[%(ErrorInd)s] %(ErrorStr)s (%(DestHost)s)' % vars(sess))
44 return filter(None, retval)[0]
45
46
47
48def main(hostfilter=None):
49 for host in gformat.get_hostlist():
50 if hostfilter and not hostfilter in host:
51 continue
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]
56 if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']:
57 addr = ifacedump['ns_ip'].split('/')[0]
58 print "## Bridge IP: %s" % addr
59 try:
60 socket.create_connection((addr,80),2)
61 bridge_type = get_bridge_type(addr)
62 datadump[iface_key]['bridge_type'] = bridge_type
63 except (socket.timeout, socket.error) as e:
64 print "### %s (%s)" % (e, addr)
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__':
73 if len(sys.argv) == 2:
74 main(hostfilter=sys.argv[1])
75 else:
76 main()
77
78
79
80
81
82
83
Note: See TracBrowser for help on using the repository browser.