| 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 | def main():
|
|---|
| 48 | for host in gformat.get_hostlist():
|
|---|
| 49 | print "# Processing host", host
|
|---|
| 50 | datadump = gformat.get_yaml(host)
|
|---|
| 51 | for iface_key in datadump['autogen_iface_keys']:
|
|---|
| 52 | ifacedump = datadump[iface_key]
|
|---|
| 53 | if ifacedump.has_key('ns_ip'):
|
|---|
| 54 | addr = ifacedump['ns_ip'].split('/')[0]
|
|---|
| 55 | print "## Bridge IP: %s" % addr
|
|---|
| 56 | try:
|
|---|
| 57 | socket.create_connection((addr,80),2)
|
|---|
| 58 | bridge_type = get_bridge_type(addr)
|
|---|
| 59 | datadump[iface_key]['bridge_type'] = bridge_type
|
|---|
| 60 | except (socket.timeout, socket.error):
|
|---|
| 61 | print "### Conection failed"
|
|---|
| 62 | except paramiko.AuthenticationException:
|
|---|
| 63 | print "### Conection failed (invalid username/password)"
|
|---|
| 64 | except CmdError, e:
|
|---|
| 65 | print "### Command error: %s" % e
|
|---|
| 66 | gformat.store_yaml(datadump)
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | if __name__ == '__main__':
|
|---|
| 70 | if sys.argv[1:]:
|
|---|
| 71 | for host in sys.argv[1:]:
|
|---|
| 72 | print get_bridge_type(host)
|
|---|
| 73 | else:
|
|---|
| 74 | main()
|
|---|