you can try using the below script which we have deployed.
this runs everyday and decrement the number of day each day and we have created an alert that we should be notified when it reaches 30 (month) so that we can initiate the work to reinstate the server certificate
import com.santaba.agent.groovyapi.expect.Expect;
import com.santaba.agent.groovyapi.snmp.Snmp;
import com.santaba.agent.groovyapi.http.*;
import com.santaba.agent.groovyapi.jmx.*;
import org.xbill.DNS.*;
import javax.net.ssl.*
import java.security.cert.*
import java.security.*
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
// Function to get the number of days left until the certificate expires
def getDaysUntilExpiry(host, port) {
try {
// Create SSL context
SSLContext sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, null, new java.security.SecureRandom())
SSLSocketFactory factory = sslContext.getSocketFactory()
// Create socket to connect to the server
Socket socket = factory.createSocket(host, port)
SSLSocket sslSocket = (SSLSocket) socket
// Start handshake to get the certificate
sslSocket.startHandshake()
SSLSession session = sslSocket.getSession()
Certificate[] certificates = session.getPeerCertificates()
X509Certificate cert = (X509Certificate) certificates[0]
// Get expiry date
Date expiryDate = cert.getNotAfter()
Date currentDate = new Date()
// Calculate days left
long diffInMillies = expiryDate.getTime() - currentDate.getTime()
long diffInDays = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS)
println("${diffInDays}")
// Close resources
sslSocket.close()
} catch (Exception e) {
e.printStackTrace()
}
}
// Main execution
def host = "website.com" // Replace with your server hostname or IP
def port = 443 // Port number
getDaysUntilExpiry(host, port)