No announcement yet.

Check certificates by openssl, load hosts from file

  • Filter
  • Time
  • Show
Clear All
new posts

  • Check certificates by openssl, load hosts from file

    Hi all,
    I have a script which can check server certificate expiry and get result how many days left.
    (=script download cert, save to file,convert and check date)
    Problem is that I have a lot of servers to check.
    Is here any way how rewrite code to load server's IPs from text file? For example: line1 in server.txt: "some_description some_hostname IP_address_port"; Line2: same structure, different values. Always separate by "gap". I need load just Ip_address_port from end of each line to check expiry for every server(every line) in servers.txt
    Can I do this with Split function or some simmilar? How put this into code?

    Other question is, when script find certificate with specify conditions, 20 days left to expiry or cert is expired eg., if is possible write line to other file (output.tx) with name of certificate (load CN from txt file?) and with line from server.txt ? I mean main output from script will be output.txt where I can see which certificates will expiry and when or which certificates are expired already.

    Thanks for suggestions!

    Option Explicit
    Dim oShell
    Dim Host
    Dim Fh
    Dim FSO
    Dim Line
    Dim Expiry
    Dim DaysLeft
    Dim ExpDate
    Dim oExec
    Dim cert
    Dim tstream
    Dim i
    Dim str
    Dim PluginPath
    Dim CertFilePath
    Dim char
    Dim XString
    Const ForReading = 1
    CertFilePath = "C:\cert.txt"
    PluginPath = "C:\Program Files\OpenSSL-win64\bin\"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set tstream = FSO.createtextfile(CertFilePath, True)
    Host =
    Set oShell = wscript.CreateObject("WScript.Shell")
    Set oExec = oShell.exec(PluginPath & "openssl.exe s_client -connect " & Host)
    Do Until oExec.StdOut.AtEndOfStream
        char = oExec.StdOut.Read(1)
        tstream.Write (char)
        If Len(XString) < 20 Then
            XString = XString & char
        ElseIf XString = "END CERTIFICATE-----" Then
    Exit Do
            XString = Mid(XString, 2, 19)
            XString = XString & char
        End If
    Set oExec = oShell.exec(PluginPath & "openssl.exe x509 -noout -in " & CertFilePath & " -dates")
    Line = oExec.StdOut.Readall
    Expiry = (Mid(Line, Len(Line) - 24, 24))
    ExpDate = ConvertDate(Expiry)
    DaysLeft = DateDiff("d", Now(), ExpDate)
    wscript.echo "Statistic: " & DaysLeft
    wscript.echo "Message:  Number of days remaning of expiry for SSL certificate are " & DaysLeft
    Set oShell = Nothing
    Set oExec = Nothing
    Function ConvertDate(DateStr)
    Dim Components
    Dim Month
    Dim Day
    Dim Year
    Components = Split(DateStr)
    Select Case Components(0)
        Case "Jan", "January"
        Month = 1
        Case "Feb", "February"
        Month = 2
        Case "Mar", "March"
        Month = 3
        Case "Apr", "April"
        Month = 4
        Case "May"
        Month = 5
        Case "Jun", "June"
        Month = 6
        Case "Jul", "July"
        Month = 7
        Case "Aug", "August"
        Month = 8
        Case "Sep", "Sept", "September"
        Month = 9
        Case "Oct", "October"
        Month = 10
        Case "Nov", "November"
        Month = 11
        Case "Dec", "December"
        Month = 12
    Case Else
        Month = 1
    End Select
        Day = Components(1)
        Year = Components(3)
    If UBound(Components) = 5 Then
        Day = Components(2)
        Year = Components(4)
        Day = Components(1)
        Year = Components(3)
    End If
    ConvertDate = CDate(Day & "/" & Month & "/" & Year)
    End Function