@@ -13,10 +13,11 @@ import (
13
13
)
14
14
15
15
type Client struct {
16
- Host string
17
- User string
18
- Password string
19
- PrivateKeyPath string
16
+ Host string
17
+ User string
18
+ Password string
19
+ PrivateKeyPath string
20
+ PrivateKeyPassphrase string
20
21
* ssh.Client
21
22
}
22
23
@@ -29,7 +30,7 @@ func (sshClient *Client) Connect() error {
29
30
if sshClient .Password != "" {
30
31
conf .Auth = append (conf .Auth , ssh .Password (sshClient .Password ))
31
32
} else if sshClient .PrivateKeyPath != "" {
32
- privateKey , err := getPrivateKey (sshClient .PrivateKeyPath )
33
+ privateKey , err := getPrivateKey (sshClient .PrivateKeyPath , sshClient . PrivateKeyPassphrase )
33
34
if err != nil {
34
35
return err
35
36
}
@@ -65,7 +66,7 @@ func (sshClient *Client) Close() {
65
66
}
66
67
67
68
// Get the private key for current user
68
- func getPrivateKey (privateKeyPath string ) (ssh.AuthMethod , error ) {
69
+ func getPrivateKey (privateKeyPath string , privateKeyPassphrase string ) (ssh.AuthMethod , error ) {
69
70
if ! fileExist (privateKeyPath ) {
70
71
defaultPrivateKeyPath := filepath .Join (os .Getenv ("HOME" ), ".ssh/id_rsa" )
71
72
log .Printf ("Warning: private key path [%s] does not exist, using default %s instead" , privateKeyPath , defaultPrivateKeyPath )
@@ -78,7 +79,12 @@ func getPrivateKey(privateKeyPath string) (ssh.AuthMethod, error) {
78
79
return nil , fmt .Errorf ("unable to parse private key: %v" , err )
79
80
}
80
81
81
- signer , err := ssh .ParsePrivateKey (key )
82
+ var signer ssh.Signer
83
+ if privateKeyPassphrase != "" {
84
+ signer , err = ssh .ParsePrivateKeyWithPassphrase (key , []byte (privateKeyPassphrase ))
85
+ } else {
86
+ signer , err = ssh .ParsePrivateKey (key )
87
+ }
82
88
if err != nil {
83
89
return nil , fmt .Errorf ("parse private key failed: %v" , err )
84
90
}
0 commit comments