Running SSH Commands with ED25519 Key and Host Key Verification in Go
Here's how to run a command on a remote server using SSH with an ED25519 key and verify the server's fingerprint using the golang.org/x/crypto/ssh package:
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"net"
"os"
)
func main() {
// Remote server details
host := "your.server.com"
port := "22"
user := "yourusername"
command := "ls -l" // Example command to run
// Path to your private key file
privateKeyPath := "/path/to/your/private/key"
// Expected server fingerprint (SHA256)
expectedFingerprint := "SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU"
// Read the private key
privateKey, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
log.Fatalf("Failed to read private key: %v", err)
}
// Parse the private key
signer, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
// Create SSH client config
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// Get the fingerprint of the remote key
fingerprint := ssh.FingerprintSHA256(key)
// Compare with expected fingerprint
if fingerprint != expectedFingerprint {
return fmt.Errorf("host key verification failed: expected %s, got %s",
expectedFingerprint, fingerprint)
}
return nil
},
HostKeyAlgorithms: []string{
ssh.KeyAlgoED25519,
},
}
// Connect to the server
client, err := ssh.Dial("tcp", net.JoinHostPort(host, port), config)
if err != nil {
log.Fatalf("Failed to dial: %v", err)
}
defer client.Close()
// Create a session
session, err := client.NewSession()
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
defer session.Close()
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
log.Fatalf("Request for pseudo terminal failed: %v", err)
}
// Run the command
output, err := session.CombinedOutput(command)
if err != nil {
log.Fatalf("Failed to run command: %v", err)
}
fmt.Printf("Command output:\n%s", output)
}
Key Points:
-
Host Key Verification: The HostKeyCallback function verifies the server's fingerprint against the expected value.
-
ED25519 Key Support: The HostKeyAlgorithms is set to prefer ED25519 keys.
-
Private Key Authentication: The code reads and parses your ED25519 private key for authentication.
-
Command Execution: The example runs a simple ls -l command, but you can replace this with any command you need.
Notes:
- Make sure to replace the placeholder values (
your.server.com, yourusername, /path/to/your/private/key) with your actual values.
- The fingerprint format should match exactly what you get from
ssh-keygen -lf on the server.
- For production use, you might want to add more error handling and logging.
This approach provides secure authentication and host verification using modern cryptographic standards.