0

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
t0m
  • 17
  • 1
  • 8
  • 1
    `get_server_certificate` wants `(server, port)` and you are giving it a string `"('www.google.com',443)"`. The same error can be reproduced by `import ssl; ssl.get_server_certificate("('www.google.com',443)")`. You need to extract servername and port from string. – KamilCuk Jul 20 '18 at 12:49

1 Answers1

1
  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()
KamilCuk
  • 120,984
  • 8
  • 59
  • 111