Last active
May 4, 2022 09:02
-
-
Save transacid/de4ecbf8c2f81bde30b296b0e083455c to your computer and use it in GitHub Desktop.
prints when a cert expires
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"log" | |
"os" | |
"time" | |
) | |
func main() { | |
var host, port string | |
if len(os.Args) == 3 { | |
host = os.Args[1] | |
port = os.Args[2] | |
} else { | |
fmt.Println("Please specify Host and Port.") | |
os.Exit(1) | |
} | |
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%s", host, port), &tls.Config{}) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer conn.Close() | |
certs := conn.ConnectionState().PeerCertificates | |
loc, _ := time.LoadLocation("Europe/Berlin") | |
var cert time.Time | |
for _, c := range certs { | |
fmt.Println(c.NotAfter.In(loc), "\t", c.Subject) | |
if !c.IsCA { | |
cert = c.NotAfter | |
} | |
} | |
diff := time.Until(cert) | |
if diff < 24*time.Hour { | |
fmt.Printf("\nExpires in: \033[1;31m%.0f\033[0m hours.\n", diff.Hours()) | |
} else { | |
fmt.Printf("\nExpires in: \033[1;32m%.0f\033[0m days.\n", diff.Hours()/24) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment