[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 | #
|
---|
| 8 | import gformat
|
---|
[10081] | 9 | import netsnmp
|
---|
| 10 | import paramiko
|
---|
| 11 | import socket
|
---|
[10074] | 12 | import sys
|
---|
| 13 |
|
---|
[10081] | 14 | netsnmp.verbose = 0
|
---|
| 15 |
|
---|
[10074] | 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())
|
---|
[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] | 34 | def 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:
|
---|
| 43 | raise CmdError('[%(ErrorInd)s] %(ErrorStr)s' % vars(sess))
|
---|
[10081] | 44 | return filter(None, retval)[0]
|
---|
[10074] | 45 |
|
---|
[10075] | 46 |
|
---|
[10082] | 47 |
|
---|
[10074] | 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)
|
---|
[10075] | 59 | bridge_type = get_bridge_type(addr)
|
---|
| 60 | datadump[iface_key]['bridge_type'] = bridge_type
|
---|
[10074] | 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:]:
|
---|
[10081] | 73 | print get_bridge_type(host)
|
---|
| 74 | else:
|
---|
| 75 | main()
|
---|
[10083] | 76 |
|
---|
[10084] | 77 |
|
---|
[10085] | 78 |
|
---|
[10086] | 79 |
|
---|
[10087] | 80 |
|
---|
[10088] | 81 |
|
---|