diff --git a/functions.tf b/functions.tf index 6c0a98b6100882bd8453f1187cbd753a3fe3ec17..c5e13e35ab321b3a79a3df02a7b9f4dc0269e7c0 100644 --- a/functions.tf +++ b/functions.tf @@ -13,6 +13,7 @@ locals { "kodi", "transmission", "vouch", + "ssh", ] function_names = [for name in local.functions : "${local.function_name_prefix}-${replace(name, "_", "")}"] } diff --git a/src/ssh.py b/src/ssh.py new file mode 100644 index 0000000000000000000000000000000000000000..17bc62ce732ebb253f92c749bc655203fb52c640 --- /dev/null +++ b/src/ssh.py @@ -0,0 +1,16 @@ +from utils import check_ssh, publish + + +def handler(event, context): # pylint: disable=unused-argument + """Lambda event handler.""" + for host in ["ns1.shore.co.il", "ns4.shore.co.il"]: + if check_ssh(host): + print(f"SSH on {host} is OK.") + else: + message = f"SSH on {host} failed." + print(message) + publish(message) + + +if __name__ == "__main__": + handler("event", "context") diff --git a/src/utils.py b/src/utils.py index ca7e1e9ec177ee2c6fe997c68cb33c8f84bec110..2abfb34495d9dc38ce40a2513f272a97b2274da5 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,5 +1,6 @@ # pylint: disable=import-error import os +import socket import boto3 import requests @@ -36,3 +37,15 @@ def check_urls(checks): message = f"Failed check for {check['url']}." print(message) publish(message) + + +def check_ssh(host, port=22): + """Check that an SSH server is available on that host and port.""" + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + sock.connect((host, port)) + msg = sock.recv(1024) + return msg.startswith(b"SSH-2.0-OpenSSH") + except Exception as e: # pylint: disable=broad-except,invalid-name + print(str(e)) + return False