#!/usr/bin/env python

import sys
import ssl
from datetime import datetime
import pytz
import OpenSSL
import socket
from netaddr import *

def get_enddate( ip ):
    color = {
            False: "\033[31;1m",
            True: "\033[32;1m",
            'end': "\033[0m",
            'error': "\033[33;1m",
            }
    ctx = OpenSSL.SSL.Context(ssl.PROTOCOL_TLSv1)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    x509 = None

    try:
        s.connect((ip,443))
        cnx = OpenSSL.SSL.Connection(ctx, s)
        cnx.set_tlsext_host_name(ip)
        cnx.set_connect_state()
        cnx.do_handshake()
        x509 = cnx.get_peer_certificate()
        s.close()
    except Exception as e:
        print "%30s: %s%s%s" % (ip, color['error'], e, color['end'])

    issuer = x509.get_issuer()
    issuer_corp = x509.get_issuer().organizationName
    issuer_url = x509.get_issuer().organizationalUnitName
    issuer_x509 = x509.get_issuer().commonName
    server_name = x509.get_subject().commonName
#    print "issuer:" + issuer
    now = datetime.now(pytz.utc)
    begin = datetime.strptime(x509.get_notBefore(), "%Y%m%d%H%M%SZ").replace(tzinfo=pytz.UTC)
    begin_ok = begin < now
    end = datetime.strptime(x509.get_notAfter(), "%Y%m%d%H%M%SZ").replace(tzinfo=pytz.UTC)
    end_ok = end > now
    print "%30s: certificate=%s begin=%s%s%s end=%s%s%s issuer=%s" % (ip, server_name, 
            color[begin_ok], begin.strftime("%d.%m.%Y"), color['end'],
            color[end_ok], end.strftime("%d.%m.%Y"), color['end'],
            issuer_corp)

def test_conn( ip ):
    try:
        socket.gethostbyaddr(ip)
    except socket.herror:
#        print "Connection failed:" + ip
        return False
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(2)
        sock.connect((ip,443))
    except socket.error:
#        print "Host up, port 443 not available for:" + ip
        return False
#    print "Connection successful:" + ip
    return True


for ip in IPNetwork('194.132.85.0/24'):
    if test_conn( str(ip) ):
        get_enddate( str(ip) )

@@ -38,16 +38,17 @@
 ort ssl%0A
+#
 from dat
@@ -69,16 +69,32 @@
 atetime%0A
+import datetime%0A
 import p
@@ -1067,16 +1067,25 @@
   now = 
+datetime.
 datetime
@@ -1099,16 +1099,90 @@
 tz.utc)%0A
+    one_month = datetime.timedelta(days=30)%0A    ok_time = now + one_month%0A
     begi
@@ -1185,16 +1185,25 @@
 begin = 
+datetime.
 datetime
@@ -1313,16 +1313,25 @@
   end = 
+datetime.
 datetime
@@ -1422,19 +1422,23 @@
 = end %3E 
-now
+ok_time
 %0A    pri
@@ -1442,18 +1442,16 @@
 print %22%25
-30
 s: certi
@@ -1514,17 +1514,16 @@
 er_name,
- 
 %0A       
@@ -2130,48 +2130,320 @@
 e%0A%0A%0A
-for ip in IPNetwork('194.132.85.0/24'):%0A
+networks = %5B'194.132.85.0/24','194.14.240.0/24','194.14.241.0/24','194.14.242.0/24','194.14.243.0/24','194.14.244.0/24','193.182.156.0/24','193.182.157.0/24','193.182.158.0/24','193.182.159.0/24','194.132.21.0/24'%5D%0A#networks = %5B'194.14.241.120/32'%5D%0Afor network in networks:%0A    for ip in IPNetwork(network):%0A    
     
@@ -2463,24 +2463,28 @@
  str(ip) ):%0A
+    
         get_

Check all nets