fix windows id_rsa file

This commit is contained in:
rinetd 2018-12-19 19:58:10 +08:00
parent 08411d78e5
commit ad63cbf4d0
2 changed files with 19 additions and 5 deletions

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"os"
"strconv" "strconv"
"time" "time"
@ -35,8 +34,8 @@ func New(cnf *Config) (client *Client, err error) {
// 1. privite key file // 1. privite key file
if len(cnf.KeyFiles) == 0 { if len(cnf.KeyFiles) == 0 {
keyPath := os.Getenv("HOME") + "/.ssh/id_rsa"
if auth, err := AuthWithPrivateKey(keyPath, cnf.Passphrase); err == nil { if auth, err := AuthWithPrivateKey(KeyFile(), cnf.Passphrase); err == nil {
clientConfig.Auth = append(clientConfig.Auth, auth) clientConfig.Auth = append(clientConfig.Auth, auth)
} }
} else { } else {
@ -128,8 +127,7 @@ func NewWithPrivateKey(Host, Port, User, Passphrase string) (client *Client, err
HostKeyCallback: ssh.InsecureIgnoreHostKey(), HostKeyCallback: ssh.InsecureIgnoreHostKey(),
} }
// 3. privite key file // 3. privite key file
keyPath := os.Getenv("HOME") + "/.ssh/id_rsa" auth, err := AuthWithPrivateKey(KeyFile(), Passphrase)
auth, err := AuthWithPrivateKey(keyPath, Passphrase)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return nil, err return nil, err

16
util.go
View File

@ -5,9 +5,25 @@ import (
"crypto" "crypto"
"encoding/hex" "encoding/hex"
"io" "io"
"log"
"os" "os"
"path"
"path/filepath"
"github.com/mitchellh/go-homedir"
) )
func KeyFile() string {
home, err := homedir.Dir()
if err != nil {
println(err.Error())
return ""
}
key := filepath.ToSlash(path.Join(home, ".ssh/id_rsa"))
log.Println(key)
return key
}
func FileExist(file string) bool { func FileExist(file string) bool {
if _, err := os.Stat(file); err != nil { if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {