I'm trying to connect to an amazon AWS linux server with a key using the [ssh][1] package of Go programming language. However the package documentation is a bit cryptic/confusing. Does anyone know how to connect through ssh using a key or at least if it's possible ? What bothers me is that in the [Dial][3] example it says
// An SSH client is represented with a ClientConn. Currently only
// the "password" authentication method is supported.
I basically want to mimic the ssh -i x.pem root@server.com behavior and execute a command inside the server ( e.g. whoami
)
You need to use
ssh.PublicKeys
to turn a list ofssh.Signers
into anssh.AuthMethod
. You can usessh.ParsePrivateKey
to get aSigner
from the pem bytes, or if you need to use an rsa, dsa or ecdsa private key, you can give those tossh.NewSignerFromKey
.Here's an example fleshed out a bit with Agent support too (since using an agent is usually the next step after simply using a key file).
Here is an example to run ls remotely using your "plain private key file".