t0m
t0m

Reputation: 17

python automatic openssl check

With my script I want to check multiple SSL certificates from different web services. The url of each service should be read from a file. If I use adr in the get_server_certificate funtion it will work, but if want to use list_adr it will produce the error below.
My code:

import OpenSSL
import ssl

class certcheck_test:
#adr = ('www.google.com', 443)
    while 1:
        f = open("C:/tmp/test.txt", "r")
        list_adr = f.readline()
        f.close()
        print list_adr
        cnt = 0
        cert = ssl.get_server_certificate(list_adr)
        x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
        data = x509.get_notAfter()

The test.txt file:

('www.google.com',443)
('www.google.com',443)
('www.google.com',443)

The error:

  File "####/PycharmProjects/Zertifikat/certcheck_test.py", line 4, in <module>
    class certcheck_test:
  File "####/certcheck_test.py", line 12, in certcheck_test
    cert = ssl.get_server_certificate(list_adr)
  File "####\Python27\Lib\ssl.py", line 1008, in get_server_certificate
    host, port = addr
ValueError: too many values to unpack

Upvotes: 0

Views: 892

Answers (1)

KamilCuk
KamilCuk

Reputation: 141155

  1. Read file line by line.
  2. Extract server and port name from line
  3. For each server and port run desired commands

with open("C:/tmp/test.txt", "r") as f:
    for line in f:
        server = line.split("'")[1]
        port = line.split(",")[1].split(")")[0]
        list_adr=(server, port)
        cert = ssl.get_server_certificate(list_adr)
        x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
        data = x509.get_notAfter()

Upvotes: 1

Related Questions