Changeset 10093 in genesis


Ignore:
Timestamp:
Mar 10, 2012, 8:08:58 PM (13 years ago)
Author:
rick
Message:

Much better argument parsing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/check-batch-cmd

    r10092 r10093  
    66# Rick van der Zwet <info@rickvanderzwet.nl>
    77#
     8import argparse
    89import gformat
    910import netsnmp
     11import os
    1012import paramiko
    1113import socket
    1214import sys
     15import time
     16
     17try:
     18  SSHPASS = os.environ['SSHPASS']
     19except KeyError:
     20  SSHPASS = 'SSH_NOT_SET'
    1321
    1422netsnmp.verbose = 0
     
    2230  ssh = paramiko.SSHClient()
    2331  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    24   ssh.connect(hostname, username='root', password='XXXXXXXXXX',timeout=3)
     32  ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
    2533  stdin, stdout, stderr = ssh.exec_command(cmd)
    2634  stdout = stdout.readlines()
     
    4654
    4755
    48 def main(hostfilter=None):
     56def update_hosts(filters=[]):
    4957  for host in gformat.get_hostlist():
    50     if hostfilter and not hostfilter in host:
     58    if filters and not any([f in host for f in filters]):
    5159      continue
     60
    5261    print "# Processing host", host
    5362    datadump = gformat.get_yaml(host)
     
    7079       
    7180
     81def make_output(stdout, stderr):
     82  def p(prefix, lines):
     83    return ''.join(["#%s: %s" % (prefix, line) for line in lines])
     84  output = p('STDOUT', stdout)
     85  output += p('STDERR', stderr)
     86  return output
     87
     88def ubnt_snmp(hostname):
     89  lines = """\
     90snmp.community=public
     91snmp.contact=beheer@lijst.wirelessleiden.nl
     92snmp.location=WL
     93snmp.status=enabled\
     94"""
     95  cmd = '(mca-config get /tmp/get.cfg | grep -v snmp /tmp/get.cfg; echo "%s") >/tmp/new.cfg \
     96    && mca-config activate /tmp/new.cfg' % lines
     97  ssh = paramiko.SSHClient()
     98  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     99  ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
     100  stdin, stdout, stderr = ssh.exec_command(cmd)
     101  stdout = stdout.readlines()
     102  stderr = stderr.readlines()
     103  print make_output(stdout, stderr)
     104  ssh.close()
     105
     106def ubnt_keys(hostname):
     107  keys = open('authorized_keys','r').read()
     108  ssh = paramiko.SSHClient()
     109  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     110  ssh.connect(hostname, username='root', password=SSHPASS,timeout=3)
     111  cmd = 'test -d .ssh || mkdir .ssh;\
     112    echo "%s" > .ssh/authorized_keys && \
     113    chmod 0700 .ssh && \
     114    chmod 0755 . && cfgmtd -p /etc -w' % keys
     115  stdin, stdout, stderr = ssh.exec_command(cmd)
     116  stdout = stdout.readlines()
     117  stderr = stderr.readlines()
     118  print make_output(stdout, stderr)
     119  ssh.close()
     120
    72121if __name__ == '__main__':
    73   if len(sys.argv) == 2:
    74     main(hostfilter=sys.argv[1])
    75   else:
    76     main()
     122  # create the top-level parser
     123  parser = argparse.ArgumentParser(prog='Various WL management tools')
     124  #parser.add_argument('--foo', action='store_true', help='foo help')
     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)
     133  parser_snmp.set_defaults(func='keys')
    77134
     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()
    78140
    79 
    80 
    81 
    82 
    83 
     141  if args.func == 'keys':
     142    ubnt_keys(args.host)
     143  elif args.func == 'snmp':
     144    ubnt_snmp(args.host)
     145  elif args.func == 'update':
     146    update_hosts(args.filters)
Note: See TracChangeset for help on using the changeset viewer.