Check certificates by openssl, load hosts from file

Home Forums Scripting Windows Script Host Check certificates by openssl, load hosts from file

Viewing 1 post (of 1 total)
  • Author
  • Avatar

    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!

    [FONT=courier new]Option Explicit[/FONT]

    [FONT=courier new]Dim oShell[/FONT]
    [FONT=courier new]Dim Host[/FONT]
    [FONT=courier new]Dim Fh[/FONT]
    [FONT=courier new]Dim FSO[/FONT]
    [FONT=courier new]Dim Line[/FONT]
    [FONT=courier new]Dim Expiry[/FONT]
    [FONT=courier new]Dim DaysLeft[/FONT]
    [FONT=courier new]Dim ExpDate[/FONT]
    [FONT=courier new]Dim oExec[/FONT]
    [FONT=courier new]Dim cert[/FONT]
    [FONT=courier new]Dim tstream[/FONT]
    [FONT=courier new]Dim i[/FONT]
    [FONT=courier new]Dim str[/FONT]
    [FONT=courier new]Dim PluginPath[/FONT]
    [FONT=courier new]Dim CertFilePath[/FONT]
    [FONT=courier new]Dim char[/FONT]
    [FONT=courier new]Dim XString[/FONT]

    [FONT=courier new]Const ForReading = 1[/FONT]

    [FONT=courier new]CertFilePath = “C:cert.txt”[/FONT]
    [FONT=courier new]PluginPath = “C:Program FilesOpenSSL-win64bin”[/FONT]

    [FONT=courier new]Set FSO = CreateObject(“Scripting.FileSystemObject”)[/FONT]
    [FONT=courier new]Set tstream = FSO.createtextfile(CertFilePath, True)[/FONT]

    [FONT=courier new]Host =[/FONT]

    [FONT=courier new]Set oShell = wscript.CreateObject(“WScript.Shell”)[/FONT]

    [FONT=courier new][/FONT][FONT=courier new]Set oExec = oShell.exec(PluginPath & “openssl.exe s_client -connect ” & Host)[/FONT]

    [FONT=courier new][/FONT][FONT=courier new]Do Until oExec.StdOut.AtEndOfStream[/FONT]
    [FONT=courier new] char = oExec.StdOut.Read(1)[/FONT]
    [FONT=courier new] tstream.Write (char)[/FONT]

    [FONT=courier new] If Len(XString) < 20 Then[/FONT] [FONT=courier new] XString = XString & char[/FONT] [FONT=courier new] ElseIf XString = "END CERTIFICATE-----" Then[/FONT] [FONT=courier new]Exit Do[/FONT] [FONT=courier new] Else[/FONT] [FONT=courier new] XString = Mid(XString, 2, 19)[/FONT] [FONT=courier new] XString = XString & char[/FONT] [FONT=courier new] End If[/FONT] [FONT=courier new]Loop[/FONT] [FONT=courier new]tstream.Close[/FONT] [FONT=courier new]oExec.Terminate[/FONT] [FONT=courier new][/FONT][FONT=courier new]Set oExec = oShell.exec(PluginPath & "openssl.exe x509 -noout -in " & CertFilePath & " -dates")[/FONT] [FONT=courier new][/FONT][FONT=courier new]Line = oExec.StdOut.Readall[/FONT] [FONT=courier new][/FONT][FONT=courier new]Expiry = (Mid(Line, Len(Line) - 24, 24))[/FONT] [FONT=courier new]ExpDate = ConvertDate(Expiry)[/FONT] [FONT=courier new][/FONT][FONT=courier new]DaysLeft = DateDiff("d", Now(), ExpDate)[/FONT] [FONT=courier new][/FONT][FONT=courier new]wscript.echo "Statistic: " & DaysLeft[/FONT] [FONT=courier new]wscript.echo "Message: Number of days remaning of expiry for SSL certificate are " & DaysLeft[/FONT] [FONT=courier new][/FONT][FONT=courier new]oExec.Terminate[/FONT] [FONT=courier new]Set oShell = Nothing[/FONT] [FONT=courier new]Set oExec = Nothing[/FONT] [FONT=courier new][/FONT][FONT=courier new]Function ConvertDate(DateStr)[/FONT] [FONT=courier new]Dim Components[/FONT] [FONT=courier new]Dim Month[/FONT] [FONT=courier new]Dim Day[/FONT] [FONT=courier new]Dim Year[/FONT] [FONT=courier new]Components = Split(DateStr)[/FONT] [FONT=courier new]Select Case Components(0)[/FONT] [FONT=courier new] Case "Jan", "January"[/FONT] [FONT=courier new] Month = 1[/FONT] [FONT=courier new] Case "Feb", "February"[/FONT] [FONT=courier new] Month = 2[/FONT] [FONT=courier new] Case "Mar", "March"[/FONT] [FONT=courier new] Month = 3[/FONT] [FONT=courier new] Case "Apr", "April"[/FONT] [FONT=courier new] Month = 4[/FONT] [FONT=courier new] Case "May"[/FONT] [FONT=courier new] Month = 5[/FONT] [FONT=courier new] Case "Jun", "June"[/FONT] [FONT=courier new] Month = 6[/FONT] [FONT=courier new] Case "Jul", "July"[/FONT] [FONT=courier new] Month = 7[/FONT] [FONT=courier new] Case "Aug", "August"[/FONT] [FONT=courier new] Month = 8[/FONT] [FONT=courier new] Case "Sep", "Sept", "September"[/FONT] [FONT=courier new] Month = 9[/FONT] [FONT=courier new] Case "Oct", "October"[/FONT] [FONT=courier new] Month = 10[/FONT] [FONT=courier new] Case "Nov", "November"[/FONT] [FONT=courier new] Month = 11[/FONT] [FONT=courier new] Case "Dec", "December"[/FONT] [FONT=courier new] Month = 12[/FONT] [FONT=courier new]Case Else[/FONT] [FONT=courier new] Month = 1[/FONT] [FONT=courier new]End Select[/FONT] [FONT=courier new] Day = Components(1)[/FONT] [FONT=courier new] Year = Components(3)[/FONT] [FONT=courier new]If UBound(Components) = 5 Then[/FONT] [FONT=courier new] Day = Components(2)[/FONT] [FONT=courier new] Year = Components(4)[/FONT] [FONT=courier new]Else[/FONT] [FONT=courier new] Day = Components(1)[/FONT] [FONT=courier new] Year = Components(3)[/FONT] [FONT=courier new]End If[/FONT] [FONT=courier new]ConvertDate = CDate(Day & "/" & Month & "/" & Year)[/FONT] [FONT=courier new]End Function[/FONT] [/CODE][CODE]
    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 FilesOpenSSL-win64bin”

    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

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.