改进私钥登录程序(Go 不再支持 rsa 授权模式).

Signed-off-by: Rick.Chan <cy@haoan119.com>
This commit is contained in:
Rick.Chan 2023-02-10 13:23:44 +08:00
parent 0a08dd07fd
commit 60d53bae4f
2 changed files with 15 additions and 28 deletions

View File

@ -2,7 +2,6 @@ package ssh
import (
"errors"
"fmt"
"net"
"strconv"
"time"
@ -144,32 +143,20 @@ func NewWithAgent(Host, Port, User string) (client *Client, err error) {
return &Client{SSHClient: sshClient, SFTPClient: sftpClient}, nil
}
func NewWithPrivateKey(Host, Port, User, Passphrase string) (client *Client, err error) {
clientConfig := &ssh.ClientConfig{
User: User,
Timeout: DefaultTimeout,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
func NewWithPrivateKey(host, port, user, passphrase string, keyFiles []string) (client *Client, err error) {
p, _ := strconv.Atoi(port)
// if err != nil {
// p = 22
// }
if user == "" {
user = "root"
}
// 3. privite key file
auth, err := AuthWithPrivateKey(KeyFile(), Passphrase)
if err != nil {
fmt.Println(err)
return nil, err
var config = &Config{
Host: host,
Port: p,
User: user,
KeyFiles: keyFiles,
Passphrase: passphrase,
}
clientConfig.Auth = append(clientConfig.Auth, auth)
// hostPort := config.Host + ":" + strconv.Itoa(config.Port)
sshClient, err := ssh.Dial("tcp", net.JoinHostPort(Host, Port), clientConfig)
if err != nil {
return client, errors.New("Failed to dial ssh: " + err.Error())
}
// create sftp client
var sftpClient *sftp.Client
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
return client, errors.New("Failed to conn sftp: " + err.Error())
}
return &Client{SSHClient: sshClient, SFTPClient: sftpClient}, nil
return New(config)
}

View File

@ -42,7 +42,7 @@ func MkdirAll(path string) error {
return nil
}
//Md5File 计算md5
// Md5File 计算md5
func Md5File(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {