Skip to content

Commit

Permalink
2.0 修复家目录下没有id_rsa文件时导致无法连接问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xhatt committed Jun 20, 2022
1 parent b72d0eb commit 18b4e32
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions apps/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,42 @@ func genSSHConfig(node *Node) *defaultClient {
}

var authMethods []ssh.AuthMethod

var pemBytes []byte
if node.Key == "" {
pemBytes, err = ioutil.ReadFile(path.Join(u.HomeDir, ".ssh/id_rsa"))
} else {
if strings.HasPrefix(node.Key, "~") {
node.Key = path.Join(u.HomeDir, strings.Replace(node.Key, "~", "", 1))
}
_, err = os.Stat(node.Key)
if !os.IsNotExist(err) {
pemBytes, err = ioutil.ReadFile(node.Key)
} else {
pemBytes = []byte(node.Key)
}
}
if err != nil {
l.Error(err)
fmt.Println("未找到私钥文件或读取时失败")
os.Exit(1)
} else {
var signer ssh.Signer
if node.Passphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(node.Passphrase))
password := node.password()
if password == nil {
var pemBytes []byte
if node.Key == "" {
pemBytes, err = ioutil.ReadFile(path.Join(u.HomeDir, ".ssh/id_rsa"))
} else {
signer, err = ssh.ParsePrivateKey(pemBytes)
if strings.HasPrefix(node.Key, "~") {
node.Key = path.Join(u.HomeDir, strings.Replace(node.Key, "~", "", 1))
}
_, err = os.Stat(node.Key)
if !os.IsNotExist(err) {
pemBytes, err = ioutil.ReadFile(node.Key)
} else {
pemBytes = []byte(node.Key)
}
}
if err != nil {
l.Error(err)
fmt.Println("未配置密码饼干未找到私钥文件或读取时失败,无法连接到服务器")
os.Exit(1)
} else {
authMethods = append(authMethods, ssh.PublicKeys(signer))
var signer ssh.Signer
if node.Passphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(node.Passphrase))
} else {
signer, err = ssh.ParsePrivateKey(pemBytes)
}
if err != nil {
l.Error(err)
} else {
authMethods = append(authMethods, ssh.PublicKeys(signer))
}
}
}

password := node.password()

if password != nil {
} else {
authMethods = append(authMethods, password)

}

authMethods = append(authMethods, ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
Expand Down

0 comments on commit 18b4e32

Please sign in to comment.