![]()
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
)
func main() {
ExamplePublicKeys()
}
func ExamplePublicKeys() {
//var hostKey ssh.PublicKey
// A public key may be used to authenticate against the remote
// server by using an unencrypted PEM-encoded private key file.
//
// If you have an encrypted private key, the crypto/x509 package
// can be used to decrypt it.
key, err := ioutil.ReadFile("/root/.ssh/id_rsa")
if err != nil {
log.Fatalf("unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}
//检查signer是否有信息,并输出
if signer != nil {
fmt.Println("signer: ", signer)
}
config := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
//HostKeyCallback: ssh.FixedHostKey(hostKey),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Connect to the remote server and perform the SSH handshake.
client, err := ssh.Dial("tcp", "ip:22", config)
if err != nil {
log.Fatalf("unable to connect: %v", err)
}
fmt.Println("ok")
defer client.Close()
}