Difference between revisions of "Get ssh key info via go"
Jump to navigation
Jump to search
(Created page with "``` package main import ( "bufio" "encoding/base64" "fmt" "log" "net" "os" "strconv" "strings" "golan...") |
|||
Line 65: | Line 65: | ||
defer client.Close() | defer client.Close() | ||
} | } | ||
+ | ``` | ||
+ | |||
+ | ``` | ||
+ | ./getsshkeyandver <ip> <port> | ||
``` | ``` |
Latest revision as of 04:19, 19 December 2023
package main import ( "bufio" "encoding/base64" "fmt" "log" "net" "os" "strconv" "strings" "golang.org/x/crypto/ssh" ) func KeyPrint(dialAddr string, addr net.Addr, key ssh.PublicKey) error { fmt.Printf("%s %s %s\n", strings.Split(dialAddr, ":")[0], key.Type(), base64.StdEncoding.EncodeToString(key.Marshal())) return nil } func getSshVersion(addr string) (string, error) { conn, err := net.Dial("tcp", addr) if err != nil { return "", err } defer conn.Close() go fmt.Fprint(conn, "SSH-2.0-justgettingyourversionstring\r\n") versioninfo, err := bufio.NewReader(conn).ReadString('\n') if err != nil { return "", err } if versioninfo[len(versioninfo)-1] == '\r' { versioninfo = versioninfo[:len(versioninfo)-1] } return versioninfo, nil } func main() { // var host = fmt.Sprintf("%s", os.Args[1]) var host string = os.Args[1] port, err := strconv.Atoi(os.Args[2]) if err != nil { panic(err) } sshConfig := &ssh.ClientConfig{ HostKeyCallback: KeyPrint, } addr := fmt.Sprintf("%s:%d", host, port) versioninfo, err := getSshVersion(addr) fmt.Printf("vinfo %v", versioninfo) if err != nil { log.Fatalln(err) } client, err := ssh.Dial("tcp", addr, sshConfig) return if err != nil { log.Fatalln(err) } defer client.Close() }
./getsshkeyandver <ip> <port>