|
| 1 | +#! /usr/bin/env python |
| 2 | +"""This script is very useful for when you just to do a health check on a remote server. It does the followings: |
| 3 | + - NSLOOKUP |
| 4 | + - PING to see if the site is up |
| 5 | + - Certificate/SSL/TLS info """ |
| 6 | +from urllib.request import Request, urlopen, ssl, socket |
| 7 | +from urllib.error import URLError, HTTPError |
| 8 | +import os,json,hashlib, re |
| 9 | + |
| 10 | + |
| 11 | +class ServerHealthCheck(): |
| 12 | + |
| 13 | + def __init__(self, base_url, port, tcp): |
| 14 | + self.base_url=base_url |
| 15 | + self.ip_now = self.obtain_ip() |
| 16 | + self.port=port |
| 17 | + self.tcp=tcp |
| 18 | + self.url_path = self.tcp+"://"+base_url |
| 19 | + self.ping_host() |
| 20 | + self.obtain_http_info() |
| 21 | + self.obtain_cert_info() |
| 22 | + |
| 23 | + def obtain_ip(self): |
| 24 | + print("__LOOKUP____________________________________________") |
| 25 | + currnet_ip = socket.gethostbyname(self.base_url) |
| 26 | + print("ip: "+currnet_ip) |
| 27 | + print("FQDN: "+socket.getfqdn(self.base_url)) |
| 28 | + distinct_ips = [] |
| 29 | + # 0,0,0,0 is for (family, type, proto, canonname, sockaddr) |
| 30 | + socket_info = socket.getaddrinfo(self.base_url,0,0,0,0) |
| 31 | + for result in socket_info: |
| 32 | + ns_ip = result[4][0] |
| 33 | + if distinct_ips.count(ns_ip)==0: |
| 34 | + distinct_ips.append(ns_ip) |
| 35 | + print(ns_ip) |
| 36 | + distinct_ips = list(set(distinct_ips)) |
| 37 | + return currnet_ip |
| 38 | + |
| 39 | + def ping_host(self): |
| 40 | + #ping reesult |
| 41 | + print("\n\n"+"__PING INFO____________________________________________") |
| 42 | + response = os.system("ping -c 1 " + self.ip_now) |
| 43 | + #and then check the response... |
| 44 | + if response == 0: |
| 45 | + print("server "+ self.base_url+": is up ") |
| 46 | + else: |
| 47 | + print("server "+ self.base_url+": is DOWN !!!") |
| 48 | + |
| 49 | + def obtain_http_info(self): |
| 50 | + print("__SSL/TLS INFO____________________________________________") |
| 51 | + req = Request(self.url_path) |
| 52 | + try: |
| 53 | + response = urlopen(req,context=ssl._create_unverified_context()) |
| 54 | + #htmlSource = response.read() |
| 55 | + except HTTPError as e: |
| 56 | + print('The server couldn\'t fulfill the request.') |
| 57 | + print('Error code: ', e.code) |
| 58 | + except URLError as e: |
| 59 | + print('We failed to reach a server.') |
| 60 | + print('Reason: ', e.reason) |
| 61 | + else: |
| 62 | + print("http code:"+str(response.getcode()) ) |
| 63 | + |
| 64 | + def obtain_cert_info(self): |
| 65 | + context = ssl.create_default_context() |
| 66 | + with socket.create_connection((self.base_url, self.port)) as socket_connection: |
| 67 | + with context.wrap_socket(socket_connection, server_hostname=self.base_url) as server_socket: |
| 68 | + #uncomment to print everything |
| 69 | + #print(json.dumps(server_socket.getpeercert() , indent=2, sort_keys=True)) |
| 70 | + cert_info = server_socket.getpeercert() |
| 71 | + subject = dict(x[0] for x in cert_info['subject']) |
| 72 | + issued_to = subject['commonName'] |
| 73 | + issuer = dict(x[0] for x in cert_info['issuer']) |
| 74 | + issued_by = issuer['commonName'] |
| 75 | + valid_from =cert_info['notBefore'] |
| 76 | + valid_to = cert_info['notAfter'] |
| 77 | + serial_number =cert_info['serialNumber'] |
| 78 | + der_cert = server_socket.getpeercert(False) |
| 79 | + der_cert_bin = server_socket.getpeercert(True) |
| 80 | + pem_cert = ssl.DER_cert_to_PEM_cert(server_socket.getpeercert(True)) |
| 81 | + # uncomment the below line if you want to see the actual public cert |
| 82 | + #print("certificate pub:",pem_cert) |
| 83 | + thumb_md5 = hashlib.md5(der_cert_bin).hexdigest() |
| 84 | + thumb_sha1 = hashlib.sha1(der_cert_bin).hexdigest() |
| 85 | + thumb_sha256 = hashlib.sha256(der_cert_bin).hexdigest() |
| 86 | + print("issued_to: " + issued_to) |
| 87 | + print("issued_by: " + issued_by) |
| 88 | + print("valid_from: " + valid_from) |
| 89 | + print("valid_to: " + valid_from) |
| 90 | + print("MD5: " + thumb_md5) |
| 91 | + print("SHA1: " + thumb_sha1) |
| 92 | + print("SHA256: " + thumb_sha256) |
| 93 | + print ("cipher: "+str(server_socket.cipher())) |
| 94 | + print("SSL/TLS version: "+server_socket.version()) |
| 95 | + print("serial_number: "+serial_number) |
| 96 | + # print(server_socket.shared_ciphers()) |
| 97 | + server_socket.close() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + # DO NOT USE IP |
| 102 | + |
| 103 | + host_name = input("host name ? (example github.com) \n") |
| 104 | + |
| 105 | + prt = input("port ? \n") |
| 106 | + tcp_it = "https" |
| 107 | + serverHealthCheck = ServerHealthCheck(host_name, prt, tcp_it) |
0 commit comments