- Timestamp:
- Mar 10, 2012, 8:08:58 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/check-batch-cmd
r10092 r10093 6 6 # Rick van der Zwet <info@rickvanderzwet.nl> 7 7 # 8 import argparse 8 9 import gformat 9 10 import netsnmp 11 import os 10 12 import paramiko 11 13 import socket 12 14 import sys 15 import time 16 17 try: 18 SSHPASS = os.environ['SSHPASS'] 19 except KeyError: 20 SSHPASS = 'SSH_NOT_SET' 13 21 14 22 netsnmp.verbose = 0 … … 22 30 ssh = paramiko.SSHClient() 23 31 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) 25 33 stdin, stdout, stderr = ssh.exec_command(cmd) 26 34 stdout = stdout.readlines() … … 46 54 47 55 48 def main(hostfilter=None):56 def update_hosts(filters=[]): 49 57 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]): 51 59 continue 60 52 61 print "# Processing host", host 53 62 datadump = gformat.get_yaml(host) … … 70 79 71 80 81 def 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 88 def ubnt_snmp(hostname): 89 lines = """\ 90 snmp.community=public 91 snmp.contact=beheer@lijst.wirelessleiden.nl 92 snmp.location=WL 93 snmp.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 106 def 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 72 121 if __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') 77 134 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() 78 140 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.