Created
March 18, 2017 01:23
-
-
Save robertmd/e04c5a4b0c892c4e85e3296ca3dfe630 to your computer and use it in GitHub Desktop.
SSH Private key with Passphrase Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/x509" | |
"encoding/pem" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"github.com/howeyc/gopass" | |
"golang.org/x/crypto/ssh" | |
) | |
func main() { | |
fmt.Println("test") | |
b, err := ioutil.ReadFile("test") | |
if err != nil { | |
log.Fatalln("could not load file") | |
} | |
pemBlock, _ := pem.Decode(b) | |
pass, err := gopass.GetPasswd() | |
fmt.Println(string(pass)) | |
DecryptedPEMBlock, err := x509.DecryptPEMBlock(pemBlock, pass) | |
r, err := x509.ParsePKCS1PrivateKey(DecryptedPEMBlock) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
signer, err := ssh.NewSignerFromKey(r) | |
if err != nil { | |
log.Fatal(err) | |
} | |
auths := []ssh.AuthMethod{ssh.PublicKeys(signer)} | |
cfg := &ssh.ClientConfig{ | |
User: "app-admin", | |
Auth: auths, | |
} | |
client, err := ssh.Dial("tcp", "localhost:2020", cfg) | |
if err != nil { | |
fmt.Println("yo") | |
log.Fatal(err) | |
} | |
session, err := client.NewSession() | |
if err != nil { | |
fmt.Println("bo") | |
log.Fatal(err) | |
} | |
session.Run("touch test") | |
log.Println("we have a session!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment