code-dumps/py/ldap-monitor.py
2024-03-06 09:51:53 +08:00

34 lines
894 B
Python

import json
import socket
import boto3
def lambda_handler(event, context):
hosts=['10.129.72.63', '10.135.72.66', '10.129.72.64', '10.135.72.67']
port=636
timeout_seconds=1
test_results = 0
for host in hosts:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout_seconds)
result = sock.connect_ex((host,int(port)))
if result == 0:
print("Host {}:{} - Up".format(host, port))
test_results += 1
else:
print("Host {}:{} - Down".format(host, port))
sock.close()
if test_results == 4:
return {
'message' : 'Successfully connected to all LDAP servers'
}
else:
raise Exception('Not all LDAP servers can be connected!')
return {
'statusCode': 200,
'body': json.dumps("Finished")
}