From ee5a88289b04fa3c1ffe701e246d6bb01ce7dc7d Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Mon, 14 Mar 2016 07:17:16 +0100 Subject: [PATCH 1/2] Make PrintDefaults if no arg a little bit more flexible --- certchk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/certchk.go b/certchk.go index 245bed6..149967c 100644 --- a/certchk.go +++ b/certchk.go @@ -66,7 +66,7 @@ func main() { // parse command-line args flag.Parse() if flag.NArg() == 0 && len(*file) == 0 { - fmt.Fprintf(os.Stderr, "Usage: certchk [-f file] servername ...\n") + fmt.Fprintf(os.Stderr, "Usage of certchk:\n") flag.PrintDefaults() os.Exit(1) } From 1ead051326f26bb508948c0b1dd9300f0d948d3b Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Mon, 14 Mar 2016 08:05:25 +0100 Subject: [PATCH 2/2] Add '-s' option for less verbose output, also: - Replace flag method to use StringVar to make accessing variables a little bit more consistent - Rename flag 'file' to 'domainFile' to be a little bit more verbose --- certchk.go | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/certchk.go b/certchk.go index 149967c..8f87d99 100644 --- a/certchk.go +++ b/certchk.go @@ -38,14 +38,24 @@ import ( ) var ( - dialer = &net.Dialer{Timeout: 5 * time.Second} - file = flag.String("f", "", "read server names from `file`") + dialer = &net.Dialer{Timeout: 5 * time.Second} + domainFile string + scriptOutput bool ) +func init() { + flag.BoolVar(&scriptOutput, "s", false, "produce less verbose output") + flag.StringVar(&domainFile, "f", "", "read server names from `file`") +} + func check(server string, width int) { conn, err := tls.DialWithDialer(dialer, "tcp", server+":443", nil) if err != nil { - fmt.Printf("%*s | %v\n", width, server, err) + if scriptOutput { + fmt.Printf("%*s error 1970-01-01 (%v)\n", width, server, err) + } else { + fmt.Printf("%*s | %v\n", width, server, err) + } return } defer conn.Close() @@ -53,10 +63,15 @@ func check(server string, width int) { for _, c := range conn.ConnectionState().PeerCertificates { if valid == nil { - fmt.Printf("%*s | valid, expires on %s (%s)\n", width, server, - c.NotAfter.Format("2006-01-02"), humanize.Time(c.NotAfter)) + if scriptOutput { + fmt.Printf("%*s valid %s\n", width, server, + c.NotAfter.Format("2006-01-02")) + } else { + fmt.Printf("%*s | valid, expires on %s (%s)\n", width, server, + c.NotAfter.Format("2006-01-02"), humanize.Time(c.NotAfter)) + } } else { - fmt.Printf("%*s | %v\n", width, server, valid) + fmt.Printf("%*s, %v\n", width, server, valid) } return } @@ -65,7 +80,7 @@ func check(server string, width int) { func main() { // parse command-line args flag.Parse() - if flag.NArg() == 0 && len(*file) == 0 { + if flag.NArg() == 0 && len(domainFile) == 0 { fmt.Fprintf(os.Stderr, "Usage of certchk:\n") flag.PrintDefaults() os.Exit(1) @@ -83,8 +98,11 @@ func main() { } // actually check - fmt.Printf("%*s | Certificate status\n%s-+-%s\n", width, "Server", - strings.Repeat("-", width), strings.Repeat("-", 80-width-2)) + if !scriptOutput { + fmt.Printf("%*s | Certificate status\n%s-+-%s\n", width, "Server", + strings.Repeat("-", width), strings.Repeat("-", 80-width-2)) + } + for _, name := range names { check(name, width) } @@ -93,8 +111,8 @@ func main() { func getNames() (names []string) { // read names from the file - if len(*file) > 0 { - f, err := os.Open(*file) + if len(domainFile) > 0 { + f, err := os.Open(domainFile) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1)