| 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 | try:
|
|---|
| 17 | raise CmdError("rick")
|
|---|
| 18 | except (socket.timeout, socket.error):
|
|---|
| 19 | print "### Conection failed"
|
|---|
| 20 | except paramiko.AuthenticationException:
|
|---|
| 21 | print "### Conection failed (invalid username/password)"
|
|---|
| 22 | except CmdError, e:
|
|---|
| 23 | print "### Command error: %s" % e
|
|---|
| 24 |
|
|---|
| 25 | def check_host(hostname):
|
|---|
| 26 | cmd = "cat /etc/board.info"
|
|---|
| 27 |
|
|---|
| 28 | ssh = paramiko.SSHClient()
|
|---|
| 29 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|---|
| 30 | ssh.connect(hostname, username='root', password='p2nn3nk03k',timeout=3)
|
|---|
| 31 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
|---|
| 32 | stdout = stdout.readlines()
|
|---|
| 33 | stderr = stderr.readlines()
|
|---|
| 34 | ssh.close()
|
|---|
| 35 | if stderr:
|
|---|
| 36 | raise CmdError(stderr)
|
|---|
| 37 |
|
|---|
| 38 | return dict(map(lambda x: x.strip().split('='),stdout))
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | def main():
|
|---|
| 42 | for host in gformat.get_hostlist():
|
|---|
| 43 | print "# Processing host", host
|
|---|
| 44 | datadump = gformat.get_yaml(host)
|
|---|
| 45 | for iface_key in datadump['autogen_iface_keys']:
|
|---|
| 46 | ifacedump = datadump[iface_key]
|
|---|
| 47 | if ifacedump.has_key('ns_ip'):
|
|---|
| 48 | addr = ifacedump['ns_ip'].split('/')[0]
|
|---|
| 49 | print "## Bridge IP: %s" % addr
|
|---|
| 50 | try:
|
|---|
| 51 | socket.create_connection((addr,80),2)
|
|---|
| 52 | d = check_host(addr)
|
|---|
| 53 | datadump[iface_key]['bridge_type'] = d['board.name']
|
|---|
| 54 | except (socket.timeout, socket.error):
|
|---|
| 55 | print "### Conection failed"
|
|---|
| 56 | except paramiko.AuthenticationException:
|
|---|
| 57 | print "### Conection failed (invalid username/password)"
|
|---|
| 58 | except CmdError, e:
|
|---|
| 59 | print "### Command error: %s" % e
|
|---|
| 60 | gformat.store_yaml(datadump)
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | if __name__ == '__main__':
|
|---|
| 64 | if sys.argv[1:]:
|
|---|
| 65 | for host in sys.argv[1:]:
|
|---|
| 66 | check_host(host)
|
|---|
| 67 | main()
|
|---|