@@ -5,9 +5,10 @@ package vssh
5
5
6
6
import (
7
7
"fmt"
8
- "io/ioutil"
9
-
10
8
"golang.org/x/crypto/ssh"
9
+ "golang.org/x/crypto/ssh/agent"
10
+ "io/ioutil"
11
+ "net"
11
12
)
12
13
13
14
// GetConfigPEM returns SSH configuration that uses the given private key.
@@ -26,6 +27,37 @@ func GetConfigPEM(user, keyFile string) (*ssh.ClientConfig, error) {
26
27
return getConfig (user , ssh .PublicKeys (signer )), nil
27
28
}
28
29
30
+ // GetConfigPEMWithPassphrase returns SSH configuration that uses the given private key and passphrase.
31
+ func GetConfigPEMWithPassphrase (user , keyFile string , passphrase string ) (* ssh.ClientConfig , error ) {
32
+ key , err := ioutil .ReadFile (keyFile )
33
+ if err != nil {
34
+ return nil , fmt .Errorf ("unable to read private key: %v" , err )
35
+ }
36
+
37
+ signer , err := ssh .ParsePrivateKeyWithPassphrase (key , []byte (passphrase ))
38
+ if err != nil {
39
+ return nil , fmt .Errorf ("unable to parse private key: %v" , err )
40
+ }
41
+
42
+ return getConfig (user , ssh .PublicKeys (signer )), nil
43
+ }
44
+
45
+ // GetConfigSSHAgent returns SSH configuration from SSH Agent.
46
+ // default socket can get from env: os.Getenv("SSH_AUTH_SOCK")
47
+ func GetConfigSSHAgent (user , socket string ) (* ssh.ClientConfig , error ) {
48
+ conn , err := net .Dial ("unix" , socket )
49
+ if err != nil {
50
+ return nil , fmt .Errorf ("unable to connect to ssh agent: %v" , err )
51
+ }
52
+ agentconn := agent .NewClient (conn )
53
+ signers , err := agentconn .Signers ()
54
+ if err != nil {
55
+ return nil , fmt .Errorf ("unable to get signers: %v" , err )
56
+ }
57
+
58
+ return getConfig (user , ssh .PublicKeys (signers ... )), nil
59
+ }
60
+
29
61
// GetConfigUserPass returns SSH configuration that uses the given
30
62
// username and password.
31
63
func GetConfigUserPass (user , password string ) * ssh.ClientConfig {
0 commit comments