Guest User

Untitled

a guest
Jan 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import subprocess
  5. import re
  6. import paramiko
  7.  
  8.  
  9. ssh_auth = {'username':'root', 'password':'xxxx'}
  10.  
  11. if not hasattr(subprocess, 'check_output'):
  12.     def check_output(*popenargs, **kwargs):
  13.         r"""Run command with arguments and return its output as a byte string.
  14.    
  15.    Backported from Python 2.7 as it's implemented as pure python on stdlib.
  16.    
  17.    >>> check_output(['/usr/bin/python', '--version'])
  18.    Python 2.6.2
  19.    """
  20.         process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
  21.         output, unused_err = process.communicate()
  22.         retcode = process.poll()
  23.         if retcode:
  24.             cmd = kwargs.get("args")
  25.             if cmd is None:
  26.                 cmd = popenargs[0]
  27.             error = subprocess.CalledProcessError(retcode, cmd)
  28.             error.output = output
  29.             raise error
  30.         return output
  31.    
  32.     subprocess.check_output = check_output
  33.  
  34. def get_servers_from_snmpwalk(snmp_server):
  35.     snmpwalk_output = subprocess.check_output(
  36.         ('snmpwalk', '-v', '1', '-c', 'public', snmp_server))
  37.    
  38.     snmpwalk_re = re.compile(r'^.*\.389\.(?P<ip>(\d+\.){4}).* = INTEGER: 389$')
  39.     for line in snmpwalk_output.splitlines():
  40.         re_match = snmpwalk_re.match(line)
  41.         if re_match:
  42.             server = re_match.group('ip')[:-1]
  43.             if server != '0.0.0.0':
  44.                 yield server
  45.  
  46. # A set is like a list, but it makes sure the items are unique, and it does
  47. # not remember the order.
  48. servers = set()
  49.  
  50. aaa_servers = [line.strip() for line in open('aaa.conf').readlines()]
  51. for aaa_server in aaa_servers:
  52.     for server in get_servers_from_snmpwalk(aaa_server):
  53.         servers.add(server)
  54.  
  55. servers = list(servers)
  56. servers.sort()
  57.  
  58.  
  59. report_file = sys.stdout
  60. # or
  61. # report_file = open('report.txt', 'a')
  62.  
  63. def parse_ldap_conf(lines):
  64.     data = {}
  65.     for line in lines:
  66.         key, sep, value = line.strip().partition(' ')
  67.         if key:
  68.             data[key] = value
  69.     return data
  70.  
  71. for server in servers:
  72.     ssh = paramiko.SSHClient()
  73.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  74.     ssh.connect(server, **ssh_auth)
  75.     sftp = ssh.open_sftp()
  76.     ldap_conf_lines = sftp.open('/etc/ldap.conf').readlines()
  77.    
  78.     ssh.close()
  79.    
  80.     ldap_conf_data = parse_ldap_conf(ldap_conf_lines)
  81.    
  82.     report_file.write('hostname: %s, bind_timelimit: %s, timelimit: %s\n' %
  83.                       (server['hostname'],
  84.                        ldap_conf_data['bind_timelimit'],
  85.                        ldap_conf_data['timelimit'],
  86.                       ))
  87.    
  88.  
  89. report_file.close()
Add Comment
Please, Sign In to add comment