diff --git a/client.go b/client.go index e63b1c5..8746911 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net" - "os" "strconv" "time" @@ -35,8 +34,8 @@ func New(cnf *Config) (client *Client, err error) { // 1. privite key file 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) } } else { @@ -128,8 +127,7 @@ func NewWithPrivateKey(Host, Port, User, Passphrase string) (client *Client, err HostKeyCallback: ssh.InsecureIgnoreHostKey(), } // 3. privite key file - keyPath := os.Getenv("HOME") + "/.ssh/id_rsa" - auth, err := AuthWithPrivateKey(keyPath, Passphrase) + auth, err := AuthWithPrivateKey(KeyFile(), Passphrase) if err != nil { fmt.Println(err) return nil, err diff --git a/util.go b/util.go index da26b8b..92b8878 100644 --- a/util.go +++ b/util.go @@ -5,9 +5,25 @@ import ( "crypto" "encoding/hex" "io" + "log" "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 { if _, err := os.Stat(file); err != nil { if os.IsNotExist(err) {