[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 | #
|
---|
[10093] | 8 | import argparse
|
---|
[10074] | 9 | import gformat
|
---|
[10098] | 10 | import getpass
|
---|
[10081] | 11 | import netsnmp
|
---|
[10093] | 12 | import os
|
---|
[10081] | 13 | import paramiko
|
---|
| 14 | import socket
|
---|
[10074] | 15 | import sys
|
---|
[10093] | 16 | import time
|
---|
[10074] | 17 |
|
---|
[10098] | 18 | SSHPASS = None
|
---|
[10081] | 19 | netsnmp.verbose = 0
|
---|
| 20 |
|
---|
[10074] | 21 | class CmdError(Exception):
|
---|
| 22 | pass
|
---|
| 23 |
|
---|
| 24 | def check_host(hostname):
|
---|
| 25 | cmd = "cat /etc/board.info"
|
---|
| 26 |
|
---|
| 27 | ssh = paramiko.SSHClient()
|
---|
| 28 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
[10093] | 29 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
[10074] | 30 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
| 31 | stdout = stdout.readlines()
|
---|
| 32 | stderr = stderr.readlines()
|
---|
| 33 | ssh.close()
|
---|
| 34 | if stderr:
|
---|
| 35 | raise CmdError(stderr)
|
---|
| 36 |
|
---|
| 37 | return dict(map(lambda x: x.strip().split('='),stdout))
|
---|
| 38 |
|
---|
[10075] | 39 | def get_bridge_type(host):
|
---|
[10081] | 40 | """ Both NS and NS Mx uses a slighly different OID"""
|
---|
| 41 | var_list = netsnmp.VarList(
|
---|
| 42 | *map(lambda x: netsnmp.Varbind(x),
|
---|
| 43 | ['.1.2.840.10036.3.1.2.1.3.6', '.1.2.840.10036.3.1.2.1.3.7']))
|
---|
[10075] | 44 |
|
---|
[10099] | 45 | sess = netsnmp.Session(Version=1, DestHost=host, Community='public', Timeout=2 * 100000, Retries=1)
|
---|
[10075] | 46 | retval = sess.get(var_list)
|
---|
| 47 | if sess.ErrorInd < 0:
|
---|
[10098] | 48 | raise CmdError('SNMP Failed -- [%(ErrorInd)s] %(ErrorStr)s (%(DestHost)s)' % vars(sess))
|
---|
[10081] | 49 | return filter(None, retval)[0]
|
---|
[10074] | 50 |
|
---|
[10075] | 51 |
|
---|
[10082] | 52 |
|
---|
[10093] | 53 | def update_hosts(filters=[]):
|
---|
[10074] | 54 | for host in gformat.get_hostlist():
|
---|
[10094] | 55 | if filters and not any([f.lower() in host.lower() for f in filters]):
|
---|
[10092] | 56 | continue
|
---|
[10093] | 57 |
|
---|
[10074] | 58 | print "# Processing host", host
|
---|
| 59 | datadump = gformat.get_yaml(host)
|
---|
| 60 | for iface_key in datadump['autogen_iface_keys']:
|
---|
| 61 | ifacedump = datadump[iface_key]
|
---|
[10092] | 62 | if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']:
|
---|
[10074] | 63 | addr = ifacedump['ns_ip'].split('/')[0]
|
---|
[10102] | 64 | print "## Bridge IP: %(ns_ip)s at %(interface)s" % ifacedump
|
---|
[10074] | 65 | try:
|
---|
| 66 | socket.create_connection((addr,80),2)
|
---|
[10075] | 67 | bridge_type = get_bridge_type(addr)
|
---|
| 68 | datadump[iface_key]['bridge_type'] = bridge_type
|
---|
[10092] | 69 | except (socket.timeout, socket.error) as e:
|
---|
| 70 | print "### %s (%s)" % (e, addr)
|
---|
[10074] | 71 | except paramiko.AuthenticationException:
|
---|
| 72 | print "### Conection failed (invalid username/password)"
|
---|
| 73 | except CmdError, e:
|
---|
| 74 | print "### Command error: %s" % e
|
---|
| 75 | gformat.store_yaml(datadump)
|
---|
| 76 |
|
---|
| 77 |
|
---|
[10093] | 78 | def make_output(stdout, stderr):
|
---|
| 79 | def p(prefix, lines):
|
---|
| 80 | return ''.join(["#%s: %s" % (prefix, line) for line in lines])
|
---|
| 81 | output = p('STDOUT', stdout)
|
---|
| 82 | output += p('STDERR', stderr)
|
---|
| 83 | return output
|
---|
[10083] | 84 |
|
---|
[10093] | 85 | def ubnt_snmp(hostname):
|
---|
| 86 | lines = """\
|
---|
| 87 | snmp.community=public
|
---|
| 88 | snmp.contact=beheer@lijst.wirelessleiden.nl
|
---|
| 89 | snmp.location=WL
|
---|
| 90 | snmp.status=enabled\
|
---|
| 91 | """
|
---|
[10095] | 92 | cmd = 'mca-config get /tmp/get.cfg && grep -v snmp /tmp/get.cfg > /tmp/new.cfg && echo "%s" >> /tmp/new.cfg \
|
---|
[10097] | 93 | && mca-config activate /tmp/new.cfg 1>/dev/null 2>/dev/null && echo "ALL DONE"' % lines
|
---|
[10093] | 94 | ssh = paramiko.SSHClient()
|
---|
| 95 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
| 96 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
| 97 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
| 98 | stdout = stdout.readlines()
|
---|
| 99 | stderr = stderr.readlines()
|
---|
| 100 | print make_output(stdout, stderr)
|
---|
| 101 | ssh.close()
|
---|
[10084] | 102 |
|
---|
[10093] | 103 | def ubnt_keys(hostname):
|
---|
[10094] | 104 | keys = open(os.path.join(gformat.NODE_DIR,'global_keys'),'r').read()
|
---|
[10093] | 105 | ssh = paramiko.SSHClient()
|
---|
| 106 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
| 107 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
| 108 | cmd = 'test -d .ssh || mkdir .ssh;\
|
---|
[10094] | 109 | cat > .ssh/authorized_keys && \
|
---|
[10093] | 110 | chmod 0700 .ssh && \
|
---|
[10094] | 111 | chmod 0755 . && cfgmtd -p /etc -w'
|
---|
[10093] | 112 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
[10094] | 113 | stdin.write(keys)
|
---|
| 114 | stdin.flush()
|
---|
| 115 | stdin.channel.shutdown_write()
|
---|
[10093] | 116 | stdout = stdout.readlines()
|
---|
| 117 | stderr = stderr.readlines()
|
---|
| 118 | print make_output(stdout, stderr)
|
---|
| 119 | ssh.close()
|
---|
[10085] | 120 |
|
---|
[10093] | 121 | if __name__ == '__main__':
|
---|
| 122 | # create the top-level parser
|
---|
| 123 | parser = argparse.ArgumentParser(prog='Various WL management tools')
|
---|
[10098] | 124 | parser.add_argument('--ask-pass', dest="ask_pass", action='store_true', help='Ask password if SSHPASS is not found')
|
---|
[10093] | 125 | subparsers = parser.add_subparsers(help='sub-command help')
|
---|
| 126 |
|
---|
| 127 | parser_snmp = subparsers.add_parser('snmp', help='enable SNMP on UBNT')
|
---|
| 128 | parser_snmp.add_argument('host',type=str)
|
---|
| 129 | parser_snmp.set_defaults(func='snmp')
|
---|
| 130 |
|
---|
| 131 | parser_keys = subparsers.add_parser('keys', help='add ssh keys on UBNT')
|
---|
| 132 | parser_keys.add_argument('host', type=str)
|
---|
[10094] | 133 | parser_keys.set_defaults(func='keys')
|
---|
[10086] | 134 |
|
---|
[10093] | 135 | parser_update = subparsers.add_parser('update', help='process all UBNT')
|
---|
| 136 | parser_update.add_argument('filters', default=None, nargs='*', type=str)
|
---|
| 137 | parser_update.set_defaults(func='update')
|
---|
| 138 |
|
---|
| 139 | args = parser.parse_args()
|
---|
[10087] | 140 |
|
---|
[10098] | 141 | try:
|
---|
| 142 | SSHPASS = os.environ['SSHPASS']
|
---|
| 143 | except KeyError:
|
---|
| 144 | print "#WARN: SSHPASS environ variable not found"
|
---|
| 145 | if args.ask_pass:
|
---|
| 146 | SSHPASS = getpass.getpass("WL root password: ")
|
---|
| 147 |
|
---|
| 148 |
|
---|
[10093] | 149 | if args.func == 'keys':
|
---|
| 150 | ubnt_keys(args.host)
|
---|
| 151 | elif args.func == 'snmp':
|
---|
| 152 | ubnt_snmp(args.host)
|
---|
| 153 | elif args.func == 'update':
|
---|
| 154 | update_hosts(args.filters)
|
---|