[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
|
---|
[10093] | 11 | import os
|
---|
[10081] | 12 | import paramiko
|
---|
[11053] | 13 | import subprocess
|
---|
[10081] | 14 | import socket
|
---|
[10074] | 15 | import sys
|
---|
[10093] | 16 | import time
|
---|
[11729] | 17 | import logging
|
---|
[10074] | 18 |
|
---|
[11729] | 19 | logging.basicConfig(level=logging.INFO)
|
---|
| 20 | logger = logging.getLogger(__name__)
|
---|
| 21 | logging.getLogger("paramiko").setLevel(logging.WARNING)
|
---|
| 22 |
|
---|
[10098] | 23 | SSHPASS = None
|
---|
[10892] | 24 | import netsnmp
|
---|
[10081] | 25 |
|
---|
[10074] | 26 | class CmdError(Exception):
|
---|
| 27 | pass
|
---|
| 28 |
|
---|
[10434] | 29 | class ConnectError(Exception):
|
---|
| 30 | pass
|
---|
[10433] | 31 |
|
---|
| 32 |
|
---|
[10434] | 33 |
|
---|
[10433] | 34 | def host_ssh_cmd(hostname, cmd):
|
---|
[10434] | 35 | try:
|
---|
| 36 | ssh = paramiko.SSHClient()
|
---|
| 37 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
| 38 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
| 39 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
| 40 | stdout = stdout.readlines()
|
---|
| 41 | stderr = stderr.readlines()
|
---|
| 42 | ssh.close()
|
---|
| 43 | if stderr:
|
---|
| 44 | raise CmdError((stderr, stdout))
|
---|
| 45 | return stdout
|
---|
[11053] | 46 | except (socket.error, paramiko.AuthenticationException) as e:
|
---|
[10434] | 47 | raise ConnectError(e)
|
---|
[10074] | 48 |
|
---|
[10433] | 49 | def parse_ini(lines):
|
---|
| 50 | return dict(map(lambda x: x.strip().split('='),lines))
|
---|
| 51 |
|
---|
| 52 | def ubnt_probe(hostname):
|
---|
| 53 | items = parse_ini(host_ssh_cmd(hostname, 'cat /etc/board.info'))
|
---|
| 54 | print items
|
---|
| 55 |
|
---|
| 56 |
|
---|
[10075] | 57 | def get_bridge_type(host):
|
---|
[10081] | 58 | """ Both NS and NS Mx uses a slighly different OID"""
|
---|
| 59 | var_list = netsnmp.VarList(
|
---|
| 60 | *map(lambda x: netsnmp.Varbind(x),
|
---|
[11730] | 61 | ['.1.2.840.10036.3.1.2.1.3.5', '.1.2.840.10036.3.1.2.1.3.6', '.1.2.840.10036.3.1.2.1.3.7','.1.3.6.1.2.1.1.5.0']))
|
---|
[10075] | 62 |
|
---|
[10099] | 63 | sess = netsnmp.Session(Version=1, DestHost=host, Community='public', Timeout=2 * 100000, Retries=1)
|
---|
[10075] | 64 | retval = sess.get(var_list)
|
---|
| 65 | if sess.ErrorInd < 0:
|
---|
[10098] | 66 | raise CmdError('SNMP Failed -- [%(ErrorInd)s] %(ErrorStr)s (%(DestHost)s)' % vars(sess))
|
---|
[11730] | 67 | if not filter(None, retval):
|
---|
| 68 | return None
|
---|
| 69 | else:
|
---|
| 70 | return filter(None, retval)[0]
|
---|
[10074] | 71 |
|
---|
[10075] | 72 |
|
---|
[10082] | 73 |
|
---|
[10433] | 74 | def node_check(host):
|
---|
[11053] | 75 | """ Using multiple connect methods to do some basic health checking as well"""
|
---|
| 76 |
|
---|
[10433] | 77 | print "# Processing host", host
|
---|
| 78 | datadump = gformat.get_yaml(host)
|
---|
| 79 | output = host_ssh_cmd(datadump['autogen_fqdn'], 'cat /var/run/dmesg.boot')
|
---|
[10093] | 80 |
|
---|
[10433] | 81 | # Get board Type
|
---|
| 82 | for line in [x.strip() for x in output]:
|
---|
| 83 | if line.startswith('CPU:'):
|
---|
| 84 | print line
|
---|
| 85 | elif line.startswith('Geode LX:'):
|
---|
| 86 | datadump['board'] = 'ALIX2'
|
---|
| 87 | print line
|
---|
| 88 | elif line.startswith('real memory'):
|
---|
| 89 | print line
|
---|
| 90 | elif line.startswith('Elan-mmcr'):
|
---|
| 91 | datadump['board'] = 'net45xx'
|
---|
[10892] | 92 | for iface_key in datadump['autogen_iface_keys']:
|
---|
| 93 | ifacedump = datadump[iface_key]
|
---|
| 94 | if ifacedump.has_key('ns_ip') and ifacedump['ns_ip']:
|
---|
| 95 | addr = ifacedump['ns_ip'].split('/')[0]
|
---|
| 96 | print "## Bridge IP: %(ns_ip)s at %(autogen_ifname)s" % ifacedump
|
---|
| 97 | try:
|
---|
| 98 | socket.create_connection((addr,80),2)
|
---|
| 99 | bridge_type = get_bridge_type(addr)
|
---|
| 100 | datadump[iface_key]['bridge_type'] = bridge_type
|
---|
| 101 | except (socket.timeout, socket.error) as e:
|
---|
| 102 | print "### %s (%s)" % (e, addr)
|
---|
| 103 | except paramiko.AuthenticationException:
|
---|
| 104 | print "### Conection failed (invalid username/password)"
|
---|
| 105 | except CmdError, e:
|
---|
| 106 | print "### Command error: %s" % e
|
---|
[11053] | 107 |
|
---|
| 108 | try:
|
---|
| 109 | wl_release = subprocess.check_output(['snmpget', '-Oq', '-Ov', '-c', 'public', '-v2c',
|
---|
[11729] | 110 | datadump['autogen_fqdn'], 'UCD-SNMP-MIB::ucdavis.84.4.1.2.6.119.108.45.118.101.114.1'])
|
---|
[11053] | 111 | datadump['wl_release'] = int(wl_release.replace('"',''))
|
---|
| 112 | except subprocess.CalledProcessError, ValueError:
|
---|
| 113 | pass
|
---|
[10433] | 114 | gformat.store_yaml(datadump)
|
---|
[10074] | 115 |
|
---|
[10433] | 116 |
|
---|
[10093] | 117 | def make_output(stdout, stderr):
|
---|
| 118 | def p(prefix, lines):
|
---|
| 119 | return ''.join(["#%s: %s" % (prefix, line) for line in lines])
|
---|
| 120 | output = p('STDOUT', stdout)
|
---|
| 121 | output += p('STDERR', stderr)
|
---|
| 122 | return output
|
---|
[10083] | 123 |
|
---|
[10093] | 124 | def ubnt_snmp(hostname):
|
---|
| 125 | lines = """\
|
---|
| 126 | snmp.community=public
|
---|
| 127 | snmp.contact=beheer@lijst.wirelessleiden.nl
|
---|
| 128 | snmp.location=WL
|
---|
| 129 | snmp.status=enabled\
|
---|
| 130 | """
|
---|
[10095] | 131 | cmd = 'mca-config get /tmp/get.cfg && grep -v snmp /tmp/get.cfg > /tmp/new.cfg && echo "%s" >> /tmp/new.cfg \
|
---|
[10097] | 132 | && mca-config activate /tmp/new.cfg 1>/dev/null 2>/dev/null && echo "ALL DONE"' % lines
|
---|
[10093] | 133 | ssh = paramiko.SSHClient()
|
---|
| 134 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
| 135 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
| 136 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
| 137 | stdout = stdout.readlines()
|
---|
| 138 | stderr = stderr.readlines()
|
---|
| 139 | print make_output(stdout, stderr)
|
---|
| 140 | ssh.close()
|
---|
[10084] | 141 |
|
---|
[10093] | 142 | def ubnt_keys(hostname):
|
---|
[10094] | 143 | keys = open(os.path.join(gformat.NODE_DIR,'global_keys'),'r').read()
|
---|
[10093] | 144 | ssh = paramiko.SSHClient()
|
---|
| 145 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
---|
| 146 | ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
|
---|
| 147 | cmd = 'test -d .ssh || mkdir .ssh;\
|
---|
[10094] | 148 | cat > .ssh/authorized_keys && \
|
---|
[10093] | 149 | chmod 0700 .ssh && \
|
---|
[10094] | 150 | chmod 0755 . && cfgmtd -p /etc -w'
|
---|
[10093] | 151 | stdin, stdout, stderr = ssh.exec_command(cmd)
|
---|
[10094] | 152 | stdin.write(keys)
|
---|
| 153 | stdin.flush()
|
---|
| 154 | stdin.channel.shutdown_write()
|
---|
[10093] | 155 | stdout = stdout.readlines()
|
---|
| 156 | stderr = stderr.readlines()
|
---|
| 157 | print make_output(stdout, stderr)
|
---|
| 158 | ssh.close()
|
---|
[10085] | 159 |
|
---|
[10093] | 160 | if __name__ == '__main__':
|
---|
| 161 | # create the top-level parser
|
---|
| 162 | parser = argparse.ArgumentParser(prog='Various WL management tools')
|
---|
[10098] | 163 | parser.add_argument('--ask-pass', dest="ask_pass", action='store_true', help='Ask password if SSHPASS is not found')
|
---|
[10434] | 164 | parser.add_argument('--filter', dest="use_filter", action='store_true', help='Thread the host definition as an filter')
|
---|
[10093] | 165 | subparsers = parser.add_subparsers(help='sub-command help')
|
---|
| 166 |
|
---|
[10433] | 167 | parser_snmp = subparsers.add_parser('bridge', help='UBNT Bridge Management')
|
---|
| 168 | parser_snmp.add_argument('action', type=str, choices=['keys', 'snmp', 'probe'])
|
---|
[10093] | 169 | parser_snmp.add_argument('host',type=str)
|
---|
[10433] | 170 | parser_snmp.set_defaults(func='bridge')
|
---|
[10093] | 171 |
|
---|
[10433] | 172 | parser_node = subparsers.add_parser('node', help='Proxy/Node/Hybrid Management')
|
---|
| 173 | parser_node.add_argument('action', type=str, choices=['check',])
|
---|
| 174 | parser_node.add_argument('host', type=str)
|
---|
| 175 | parser_node.set_defaults(func='node')
|
---|
[10086] | 176 |
|
---|
[10093] | 177 | args = parser.parse_args()
|
---|
[10087] | 178 |
|
---|
[10098] | 179 | try:
|
---|
| 180 | SSHPASS = os.environ['SSHPASS']
|
---|
| 181 | except KeyError:
|
---|
| 182 | print "#WARN: SSHPASS environ variable not found"
|
---|
| 183 | if args.ask_pass:
|
---|
| 184 | SSHPASS = getpass.getpass("WL root password: ")
|
---|
| 185 |
|
---|
| 186 |
|
---|
[10434] | 187 | if args.use_filter:
|
---|
| 188 | hosts = []
|
---|
| 189 | for host in gformat.get_hostlist():
|
---|
| 190 | if args.host in host:
|
---|
| 191 | hosts.append(host)
|
---|
| 192 | else:
|
---|
| 193 | hosts = [args.host]
|
---|
[10433] | 194 |
|
---|
[10434] | 195 |
|
---|
| 196 | for host in hosts:
|
---|
| 197 | try:
|
---|
| 198 | if args.func == 'bridge':
|
---|
| 199 | if args.action == 'keys':
|
---|
| 200 | ubnt_keys(host)
|
---|
| 201 | elif args.action == 'snmp':
|
---|
| 202 | ubnt_snmp(host)
|
---|
| 203 | elif args.action == 'probe':
|
---|
| 204 | ubnt_probe(host)
|
---|
| 205 | elif args.func == 'node':
|
---|
| 206 | if args.action == 'check':
|
---|
| 207 | node_check(host)
|
---|
| 208 | except ConnectError:
|
---|
| 209 | print "#ERR: Connection failed to host %s" % host
|
---|