()
| 30 | } |
| 31 | |
| 32 | func (server *Server) sshTest() bool { |
| 33 | connect := true |
| 34 | var timeCostPoint *string |
| 35 | ptr := &timeCostPoint |
| 36 | defer TimeCostPTR(time.Now(), ptr) |
| 37 | idPath := filepath.Join(os.Getenv("HOME"), ".ssh", "id_ed25519") |
| 38 | if !IsExists(idPath) { |
| 39 | idPath = filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa") |
| 40 | } |
| 41 | key, err := os.ReadFile(idPath) |
| 42 | if err != nil { |
| 43 | logger.Fatalf("unable to read private key: %v", err) |
| 44 | } |
| 45 | |
| 46 | // Create the Signer for this private key. |
| 47 | signer, err := ssh.ParsePrivateKey(key) |
| 48 | if err != nil { |
| 49 | logger.Fatalf("unable to parse private key: %v", err) |
| 50 | } |
| 51 | |
| 52 | knowHostsPath := filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts") |
| 53 | if !IsExists(knowHostsPath) { |
| 54 | knowHostsFile, err := os.Create(knowHostsPath) |
| 55 | if err != nil { |
| 56 | logger.Fatal(err) |
| 57 | } |
| 58 | defer knowHostsFile.Close() |
| 59 | } |
| 60 | |
| 61 | hostKeyCallback, err := kh.New(knowHostsPath) |
| 62 | if err != nil { |
| 63 | logger.Fatal("could not create hostkey callback function: ", err) |
| 64 | } |
| 65 | |
| 66 | config := &ssh.ClientConfig{ |
| 67 | User: server.user, |
| 68 | Auth: []ssh.AuthMethod{ |
| 69 | // Add in password check here for moar security. |
| 70 | ssh.PublicKeys(signer), |
| 71 | }, |
| 72 | HostKeyCallback: hostKeyCallback, |
| 73 | } |
| 74 | // Connect to the remote server and perform the SSH handshake. |
| 75 | client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", server.ip, server.port), config) |
| 76 | if err != nil { |
| 77 | connect = false |
| 78 | } else { |
| 79 | client.Close() |
| 80 | } |
| 81 | |
| 82 | var colorConnect string |
| 83 | if connect { |
| 84 | colorConnect = color.GreenString("true") |
| 85 | } else { |
| 86 | colorConnect = color.RedString("false") |
| 87 | } |
| 88 | result := fmt.Sprintf("ssh连接性测试: '%s@%s -p %d' %s", server.user, server.ip, server.port, colorConnect) |
| 89 | timeCostPoint = &result |
no test coverage detected