Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import subprocess
- import re
- import paramiko
- ssh_auth = {'username':'root', 'password':'xxxx'}
- if not hasattr(subprocess, 'check_output'):
- def check_output(*popenargs, **kwargs):
- r"""Run command with arguments and return its output as a byte string.
- Backported from Python 2.7 as it's implemented as pure python on stdlib.
- >>> check_output(['/usr/bin/python', '--version'])
- Python 2.6.2
- """
- process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
- output, unused_err = process.communicate()
- retcode = process.poll()
- if retcode:
- cmd = kwargs.get("args")
- if cmd is None:
- cmd = popenargs[0]
- error = subprocess.CalledProcessError(retcode, cmd)
- error.output = output
- raise error
- return output
- subprocess.check_output = check_output
- def get_servers_from_snmpwalk(snmp_server):
- snmpwalk_output = subprocess.check_output(
- ('snmpwalk', '-v', '1', '-c', 'public', snmp_server))
- snmpwalk_re = re.compile(r'^.*\.389\.(?P<ip>(\d+\.){4}).* = INTEGER: 389$')
- for line in snmpwalk_output.splitlines():
- re_match = snmpwalk_re.match(line)
- if re_match:
- server = re_match.group('ip')[:-1]
- if server != '0.0.0.0':
- yield server
- # A set is like a list, but it makes sure the items are unique, and it does
- # not remember the order.
- servers = set()
- aaa_servers = [line.strip() for line in open('aaa.conf').readlines()]
- for aaa_server in aaa_servers:
- for server in get_servers_from_snmpwalk(aaa_server):
- servers.add(server)
- servers = list(servers)
- servers.sort()
- report_file = sys.stdout
- # or
- # report_file = open('report.txt', 'a')
- def parse_ldap_conf(lines):
- data = {}
- for line in lines:
- key, sep, value = line.strip().partition(' ')
- if key:
- data[key] = value
- return data
- for server in servers:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(server, **ssh_auth)
- sftp = ssh.open_sftp()
- ldap_conf_lines = sftp.open('/etc/ldap.conf').readlines()
- ssh.close()
- ldap_conf_data = parse_ldap_conf(ldap_conf_lines)
- report_file.write('hostname: %s, bind_timelimit: %s, timelimit: %s\n' %
- (server['hostname'],
- ldap_conf_data['bind_timelimit'],
- ldap_conf_data['timelimit'],
- ))
- report_file.close()
Add Comment
Please, Sign In to add comment