[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
|
---|
| 9 | import sys
|
---|
| 10 | import socket
|
---|
| 11 | import paramiko
|
---|
| 12 |
|
---|
| 13 | class CmdError(Exception):
|
---|
| 14 | pass
|
---|
| 15 |
|
---|
| 16 | def check_host(hostname):
|
---|
| 17 | cmd = "cat /etc/board.info"
|
---|
| 18 |
|
---|
| 19 | ssh = paramiko.SSHClient()
|
---|
| 20 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
[10075] | 21 | ssh.connect(hostname, username='root', password='XXXXXXXXXX',timeout=3)
|
---|
[10074] | 22 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
| 23 | stdout = stdout.readlines()
|
---|
| 24 | stderr = stderr.readlines()
|
---|
| 25 | ssh.close()
|
---|
| 26 | if stderr:
|
---|
| 27 | raise CmdError(stderr)
|
---|
| 28 |
|
---|
| 29 | return dict(map(lambda x: x.strip().split('='),stdout))
|
---|
| 30 |
|
---|
[10075] | 31 | def get_bridge_type(host):
|
---|
| 32 | import netsnmp
|
---|
| 33 | netsnmp.verbose = 0
|
---|
| 34 | var_list = netsnmp.VarList(netsnmp.Varbind('.1.2.840.10036.3.1.2.1.3.6'))
|
---|
| 35 |
|
---|
| 36 | sess = netsnmp.Session(Version=1, DestHost=host, Community='public', Timeout=2 * 100000, Retries=0)
|
---|
| 37 | retval = sess.get(var_list)
|
---|
| 38 | if sess.ErrorInd < 0:
|
---|
| 39 | raise CmdError('[%(ErrorInd)s] %(ErrorStr)s' % vars(sess))
|
---|
| 40 | return retval[0]
|
---|
[10074] | 41 |
|
---|
[10075] | 42 |
|
---|
[10074] | 43 | def main():
|
---|
| 44 | for host in gformat.get_hostlist():
|
---|
| 45 | print "# Processing host", host
|
---|
| 46 | datadump = gformat.get_yaml(host)
|
---|
| 47 | for iface_key in datadump['autogen_iface_keys']:
|
---|
| 48 | ifacedump = datadump[iface_key]
|
---|
| 49 | if ifacedump.has_key('ns_ip'):
|
---|
| 50 | addr = ifacedump['ns_ip'].split('/')[0]
|
---|
| 51 | print "## Bridge IP: %s" % addr
|
---|
| 52 | try:
|
---|
| 53 | socket.create_connection((addr,80),2)
|
---|
[10075] | 54 | bridge_type = get_bridge_type(addr)
|
---|
| 55 | datadump[iface_key]['bridge_type'] = bridge_type
|
---|
[10074] | 56 | except (socket.timeout, socket.error):
|
---|
| 57 | print "### Conection failed"
|
---|
| 58 | except paramiko.AuthenticationException:
|
---|
| 59 | print "### Conection failed (invalid username/password)"
|
---|
| 60 | except CmdError, e:
|
---|
| 61 | print "### Command error: %s" % e
|
---|
| 62 | gformat.store_yaml(datadump)
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | if __name__ == '__main__':
|
---|
| 66 | if sys.argv[1:]:
|
---|
| 67 | for host in sys.argv[1:]:
|
---|
| 68 | check_host(host)
|
---|
| 69 | main()
|
---|