改进私钥登录程序(Go 不再支持 rsa 授权模式).
Signed-off-by: Rick.Chan <cy@haoan119.com>
This commit is contained in:
parent
0a08dd07fd
commit
60d53bae4f
41
client.go
41
client.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue