Skip to content

Commit a00687f

Browse files
committed
私钥登录增加私钥密码支持
1 parent 9f99d65 commit a00687f

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ AB两台服务器中的项目均将日志写到文件系统的`/home/data/logs/l
5353
#tail_flags="--retry --follow=name"
5454

5555
# 服务器配置,可以配置多个
56-
# 如果不提供 password, 则默认使用系统配置的 ssh-agent 设置,
56+
# 如果不提供password, 则默认使用系统配置的 ssh-agent 设置,
5757
# 你也可以通过指定 private_key_path 配置项来指定使用特定的私钥来登录 (private_key_path=/home/mylxsw/.ssh/id_rsa)
58+
# 私钥如果有密码的话,需要指定 private_key_passphrase 配置项来指定私钥密码
5859
# server_name, hostname, user 配置为必选,其它可选
5960
[servers]
6061

command/command.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ func NewCommand(server Server) (cmd *Command) {
4646
func (cmd *Command) Execute(output chan Message) {
4747

4848
client := &ssh.Client{
49-
Host: cmd.Host,
50-
User: cmd.User,
51-
Password: cmd.Server.Password,
52-
PrivateKeyPath: cmd.Server.PrivateKeyPath,
49+
Host: cmd.Host,
50+
User: cmd.User,
51+
Password: cmd.Server.Password,
52+
PrivateKeyPath: cmd.Server.PrivateKeyPath,
53+
PrivateKeyPassphrase: cmd.Server.PrivateKeyPassphrase,
5354
}
5455

5556
if err := client.Connect(); err != nil {

command/config.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package command
22

33
type Server struct {
4-
ServerName string `toml:"server_name"`
5-
Hostname string `toml:"hostname"`
6-
Port int `toml:"port"`
7-
User string `toml:"user"`
8-
Password string `toml:"password"`
9-
PrivateKeyPath string `toml:"private_key_path"`
10-
TailFile string `toml:"tail_file"`
11-
TailFlags string `toml:"tail_flags"`
4+
ServerName string `toml:"server_name"`
5+
Hostname string `toml:"hostname"`
6+
Port int `toml:"port"`
7+
User string `toml:"user"`
8+
Password string `toml:"password"`
9+
PrivateKeyPath string `toml:"private_key_path"`
10+
PrivateKeyPassphrase string `toml:"private_key_passphrase"`
11+
TailFile string `toml:"tail_file"`
12+
TailFlags string `toml:"tail_flags"`
1213
}
1314

1415
type Config struct {

ssh/ssh.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
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
2021
*ssh.Client
2122
}
2223

@@ -29,7 +30,7 @@ func (sshClient *Client) Connect() error {
2930
if sshClient.Password != "" {
3031
conf.Auth = append(conf.Auth, ssh.Password(sshClient.Password))
3132
} else if sshClient.PrivateKeyPath != "" {
32-
privateKey, err := getPrivateKey(sshClient.PrivateKeyPath)
33+
privateKey, err := getPrivateKey(sshClient.PrivateKeyPath, sshClient.PrivateKeyPassphrase)
3334
if err != nil {
3435
return err
3536
}
@@ -65,7 +66,7 @@ func (sshClient *Client) Close() {
6566
}
6667

6768
// 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) {
6970
if !fileExist(privateKeyPath) {
7071
defaultPrivateKeyPath := filepath.Join(os.Getenv("HOME"), ".ssh/id_rsa")
7172
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) {
7879
return nil, fmt.Errorf("unable to parse private key: %v", err)
7980
}
8081

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+
}
8288
if err != nil {
8389
return nil, fmt.Errorf("parse private key failed: %v", err)
8490
}

0 commit comments

Comments
 (0)