Skip to content
ssh.py 1 KiB
Newer Older
nimrod's avatar
nimrod committed
"""Check SSH."""

nimrod's avatar
nimrod committed
import socket
nimrod's avatar
nimrod committed
from utils import Check
nimrod's avatar
nimrod committed


nimrod's avatar
nimrod committed
class CheckSSH(Check):
    def __init__(self, hostname, port=22):
        self._hostname = hostname
        self._port = port

    def _check(self):
        """Check that an SSH server is available on that host and port."""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect((self._hostname, self._port))
            msg = sock.recv(1024)
            sock.close()
            if msg.startswith(b"SSH-2.0-OpenSSH"):
                return True, f"SSH on {self._hostname}:{self._port} is OK."
            return False, f"SSH on {self._hostname}:{self._port} failed."
        except Exception as e:  # pylint: disable=broad-except,invalid-name
            return False, str(e)
nimrod's avatar
nimrod committed


def handler(event, context):  # pylint: disable=unused-argument
    """Lambda event handler."""
    for host in ["ns1.shore.co.il", "ns4.shore.co.il"]:
nimrod's avatar
nimrod committed
        CheckSSH(host).run()
nimrod's avatar
nimrod committed


if __name__ == "__main__":
    handler("event", "context")