Thursday, 15 March 2012

go - Connect to a server using SSH and a pem / key with golang -



go - Connect to a server using SSH and a pem / key with golang -

i'm trying connect amazon aws linux server key using [ssh][1] bundle of go programming language. bundle documentation bit cryptic/confusing. know how connect through ssh using key or @ to the lowest degree if it's possible ? bothers me in [dial][3] illustration says

// ssh client represented clientconn. // "password" authentication method supported.

i want mimic ssh -i x.pem root@server.com behavior , execute command within server ( e.g. whoami )

you need utilize ssh.publickeys turn list of ssh.signers ssh.authmethod. can utilize ssh.parseprivatekey signer pem bytes, or if need utilize rsa, dsa or ecdsa private key, can give ssh.newsignerfromkey.

here's illustration fleshed out bit agent back upwards (since using agent next step after using key file).

sock, err := net.dial("unix", os.getenv("ssh_auth_sock")) if err != nil { log.fatal(err) } agent := agent.newclient(sock) signers, err := agent.signers() if err != nil { log.fatal(err) } // or signer private key file straight // signer, err := ssh.parseprivatekey(pembytes) // if err != nil { // log.fatal(err) // } auths := []ssh.authmethod{ssh.publickeys(signers...)} cfg := &ssh.clientconfig{ user: "username", auth: auths, } cfg.setdefaults() client, err := ssh.dial("tcp", "aws-hostname:22", cfg) if err != nil { log.fatal(err) } session, err = client.newsession() if err != nil { log.fatal(err) } log.println("we have session!") ...

ssh go

No comments:

Post a Comment