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 | #
|
---|
8 | import gformat
|
---|
9 | import netsnmp
|
---|
10 | import paramiko
|
---|
11 | import socket
|
---|
12 | import sys
|
---|
13 |
|
---|
14 | netsnmp.verbose = 0
|
---|
15 |
|
---|
16 | class CmdError(Exception):
|
---|
17 | pass
|
---|
18 |
|
---|
19 | def 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 |
|
---|
34 | def 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' % vars(sess))
|
---|
44 | return filter(None, retval)[0]
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | def main():
|
---|
49 | for host in gformat.get_hostlist():
|
---|
50 | print "# Processing host", host
|
---|
51 | datadump = gformat.get_yaml(host)
|
---|
52 | for iface_key in datadump['autogen_iface_keys']:
|
---|
53 | ifacedump = datadump[iface_key]
|
---|
54 | if ifacedump.has_key('ns_ip'):
|
---|
55 | addr = ifacedump['ns_ip'].split('/')[0]
|
---|
56 | print "## Bridge IP: %s" % addr
|
---|
57 | try:
|
---|
58 | socket.create_connection((addr,80),2)
|
---|
59 | bridge_type = get_bridge_type(addr)
|
---|
60 | datadump[iface_key]['bridge_type'] = bridge_type
|
---|
61 | except (socket.timeout, socket.error):
|
---|
62 | print "### Conection failed"
|
---|
63 | except paramiko.AuthenticationException:
|
---|
64 | print "### Conection failed (invalid username/password)"
|
---|
65 | except CmdError, e:
|
---|
66 | print "### Command error: %s" % e
|
---|
67 | gformat.store_yaml(datadump)
|
---|
68 |
|
---|
69 |
|
---|
70 | if __name__ == '__main__':
|
---|
71 | if sys.argv[1:]:
|
---|
72 | for host in sys.argv[1:]:
|
---|
73 | print get_bridge_type(host)
|
---|
74 | else:
|
---|
75 | main()
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 |
|
---|