{"id":668,"date":"2021-03-28T14:33:24","date_gmt":"2021-03-28T13:33:24","guid":{"rendered":"http:\/\/192.168.8.14\/?p=668"},"modified":"2021-03-28T15:04:51","modified_gmt":"2021-03-28T14:04:51","slug":"vcsa-cert-checking-script","status":"publish","type":"post","link":"https:\/\/www.jasonstreet.com\/?p=668","title":{"rendered":"VCSA Cert checking script"},"content":{"rendered":"\n<p>Over the past year I have seen quite a few STS certs expire in production environments. I have been meaning to write something to scan a VCSA and report back the expiry dates of all the certs. <\/p>\n\n\n\n<p>This has posed a problem because I have to use SSH and one command needs to follow another. In the end I discovered the SSHShellStream in the Posh-SSH module. I will document that (character building) journey in another post. <\/p>\n\n\n\n<p>This script will use the POSH-SSH module to connect to a VCSA and issue a one liner BASH script to get the cert info. This info is captured and processed by the script. But the STS cert info can not be captured in this way as it requires a python script to be uploaded and run. This script needs to be manually copied to the remote VCSA.<\/p>\n\n\n\n<p>Deployment of the script<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You will need the POSH-SSH module installed.<\/li><li>You will need to copy up the stscheck.ph script to the \/tmp directory of the VCSA<br>The checksts.py script is located here<br><a rel=\"noreferrer noopener\" href=\"http:\/\/192.168.8.14\/?p=634\" target=\"_blank\">My Local copy<\/a><br><a rel=\"noreferrer noopener\" href=\"https:\/\/kb.vmware.com\/s\/article\/79248\" target=\"_blank\">vmware KB<\/a><\/li><li>Set the $vcsa variable to the FQDN of your vCenter.<\/li><\/ul>\n\n\n\n<p>Thats it. you are ready to go.<\/p>\n\n\n\n<p>Running the script<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The script will connect to $vcsa using SSH and prompt for credentials.<\/li><li>Once the script has run you will have an array called $CertReport and a copy of that in c:\\temp\\CertReport.csv<\/li><\/ul>\n\n\n\n<p>You will end up with a CSV file looking very much like this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&quot;vCenter&quot;,&quot;location&quot;,&quot;Name&quot;,&quot;ExpDate&quot;,&quot;Days&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE MACHINE_SSL_CERT&quot;,&quot;15\/03\/2023 13:27:50&quot;,&quot;716&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE TRUSTED_ROOTS&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE TRUSTED_ROOTS&quot;,&quot;07\/01\/2029 13:27:50&quot;,&quot;2841&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE TRUSTED_ROOTS&quot;,&quot;08\/01\/2031 13:27:50&quot;,&quot;3573&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE TRUSTED_ROOTS&quot;,&quot;08\/01\/2031 13:27:50&quot;,&quot;3573&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE TRUSTED_ROOTS&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE machine&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3632&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE vsphere-webclient&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE vpxd&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE vpxd-extension&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE hvc&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3632&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE data-encipherment&quot;,&quot;09\/03\/2031 13:27:50&quot;,&quot;3633&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE SMS&quot;,&quot;14\/03\/2031 13:27:50&quot;,&quot;3638&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STORE wcp&quot;,&quot;14\/03\/2023 13:27:50&quot;,&quot;716&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STS Valid LEAF&quot;,&quot;13\/01\/2023 13:27:50&quot;,&quot;656&quot;\n&quot;myvCenter.mydomain.com&quot;,&quot;lab&quot;,&quot;STS Valid Root&quot;,&quot;08\/01\/2031 13:27:50&quot;,&quot;3573&quot;\n<\/pre><\/div>\n\n\n<p>Note: that the above output is from a VCSA server installed about 2 weeks ago, so All is looking good. I do not have any samples of an expired certificate (Ill update this post in 716 days) so I do not know how the script will deal with an expired Cert. But saying that, my On-call week is about to start so I may get an example. Im &#8220;lucky&#8221; like that.<\/p>\n\n\n\n<p>Anyhow, here is the script<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nimport-module posh-ssh\n\n$Cred = Get-Credential\n\n$vcsa = &#039;MyvCenterServer.Domain.com&#039;\n$location = &#039;HomeLab&#039;\n\n# connect to vcsa with SSH and open the SSh stream\n$session = New-SSHSession -ComputerName $vcsa -Credential $cred \u2013AcceptKey\n$stream = New-SSHShellStream -SSHSession $session -TerminalName tty\n\n# send the commands and get the results in to $CertList and $STSCertList\n# drop to the BASH shell\n$stream.WriteLine(&#039;shell&#039;)\nsleep 3\n# send a BASH one liner to list the crets and there info\n$stream.WriteLine(&#039;for i in $(\/usr\/lib\/vmware-vmafd\/bin\/vecs-cli store list); do echo STORE $i; sudo \/usr\/lib\/vmware-vmafd\/bin\/vecs-cli entry list --store $i --text | egrep &quot;Alias|Not After&quot;; done&#039;)\nsleep 3\n# read the result of that in to the variable $CertList\n$CertList = $stream.Read()\nsleep 3\n$stream.WriteLine(&#039;cd \/tmp&#039;)\nsleep 3\n$DontCare = $stream.Read()\nsleep 3\n# run the checksys python script in \/tmp (if that is where you have deployed it to)\n$stream.WriteLine(&#039;python checksts.py&#039;)\nsleep 3\n# get the output of that script in to the $STSCertList variable\n$STSCertList = $stream.Read()\n\n# close the SSH session and stream\n$stream.Close()\nRemove-SSHSession -SSHSession $session | Out-Null\n\n\n# $CertList and $STSCertList are txt streams, split using newline to get an array of text lines (a text file)\n$NewCertList = $CertList.split(&#x5B;Environment]::NewLine)\n$NewSTSCertList = $STSCertList.split(&#x5B;Environment]::NewLine)\n\n# loop through each line and put the relivent info in to the $CertReport array\n$CertReport = @()\nforeach($line in $NewCertList)\n{\n    if ($line -match &quot;STORE&quot;){$CurrentStore = $line}\n    if (($Line -match &quot;not After&quot;) -and ($Line -notmatch &quot;egrep&quot;))\n    {\n\n        $line = $line.replace(&quot;Not After :&quot;,&quot;&quot;)\n        $line = $line.replace(&quot;  &quot;,&quot; &quot;)\n        $line = $line.replace(&quot; GMT&quot;,&quot; &quot;)\n        $line = $line.trim()\n        $cells = $line.split(&quot; &quot;)\n        if ($cells&#x5B;0] -eq &quot;Jan&quot;){$Month = 1}\n        if ($cells&#x5B;0] -eq &quot;Feb&quot;){$Month = 2}\n        if ($cells&#x5B;0] -eq &quot;Mar&quot;){$Month = 3}\n        if ($cells&#x5B;0] -eq &quot;Apr&quot;){$Month = 4}\n        if ($cells&#x5B;0] -eq &quot;May&quot;){$Month = 5}\n        if ($cells&#x5B;0] -eq &quot;Jun&quot;){$Month = 6}\n        if ($cells&#x5B;0] -eq &quot;Jul&quot;){$Month = 7}\n        if ($cells&#x5B;0] -eq &quot;Aug&quot;){$Month = 8}\n        if ($cells&#x5B;0] -eq &quot;Sep&quot;){$Month = 9}\n        if ($cells&#x5B;0] -eq &quot;Oct&quot;){$Month = 10}\n        if ($cells&#x5B;0] -eq &quot;Nov&quot;){$Month = 11}\n        if ($cells&#x5B;0] -eq &quot;Dec&quot;){$Month = 12}\n\n        # get the exp date and convert to a date-time object\n        $day = $Cells&#x5B;1]\n        $year = $cells&#x5B;3]\n        $ExpDate = get-date -year $year -month $Month -day $day\n\n        # now get the number of days to cert expire\n        $ExpIn = $ExpDate - (get-date)\n\n        # create a tmp object with cert info and add to th result array, $CertReport\n        $tmpInfo = &#039;&#039; | select vCenter,location,Name,ExpDate,Days\n        $tmpInfo.vCenter = $vcsa\n        $tmpInfo.location = $location\n        $tmpInfo.Name = $CurrentStore\n        $tmpInfo.ExpDate = $ExpDate\n        $tmpInfo.Days = $ExpIn.days\n        $CertReport += $tmpInfo\n    }\n}\n\nforeach($line in $NewSTSCertList)\n{\n    if($line -match &quot;VALID CERTS&quot;){$CertValid = &quot;Valid&quot;}\n    if($line -match &quot;EXPIRED CERTSS&quot;){$CertValid = &quot;Expired&quot;}\n    if($line -match &quot;LEAF CERTS&quot;){$CertType = &quot;LEAF&quot;}\n    if($line -match &quot;Root CERTS&quot;){$CertType = &quot;Root&quot;}\n    if($line -match &quot;Certificate&quot;)\n    {\n        # split the line using a space as the delimiter\n        $cells = $line.split(&quot; &quot;)\n        $CellNum = 0\n        # loop through the cells, if the cell = &quot;days&quot; get the cell before it as that is the number\n        foreach ($Cell in $cells)\n        {\n            if ($cell -eq &quot;days&quot;){$ExpDays = $Cells&#x5B;$CellNum - 1] }\n            $CellNum ++\n        }\n    \n        # create a tmp object with cert info and add to th result array, $CertReport\n        $tmpInfo = &#039;&#039; | select vCenter,location,Name,ExpDate,Days\n        $tmpInfo.vCenter = $vcsa\n        $tmpInfo.location = $location\n        $tmpInfo.Name = &quot;STS &quot; + $CertValid + &quot; &quot; + $CertType\n        $tmpInfo.ExpDate = (get-date).adddays(&#x5B;int]$ExpDays)\n        $tmpInfo.Days = $ExpDays\n        $CertReport += $tmpInfo\n    }\n}\n$CertReport | export-csv &quot;C:\\temp\\certReport.csv&quot; -NoTypeInformation\n\n\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Over the past year I have seen quite a few STS certs expire in production environments. I have been meaning to write something to scan a VCSA and report back the expiry dates of all the certs. This has posed a problem because I have to use SSH and one command needs to follow another&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,4,132,43],"tags":[126,133,12,13,102,128,46],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-powercli","category-powershell","category-reporting","category-vmware","tag-certificate","tag-expire","tag-powercli","tag-powershell","tag-ssh","tag-sts","tag-vcsa"],"_links":{"self":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=668"}],"version-history":[{"count":3,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=\/wp\/v2\/posts\/668\/revisions\/671"}],"wp:attachment":[{"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasonstreet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}