From 341dcf9bd0581d89a7ca45a9706072b2f31478b5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 15 Nov 2019 17:16:54 +0400 Subject: [PATCH 1/2] Changed links to examples --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96f5cb3..8974953 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,6 @@ Do not need to think about Dials, sessions, defers and public keys...Let easyssh ## So easy to use! -[Run a command on remote server and get STDOUT output](https://github.com/hypersleep/easyssh/blob/master/example/run.go) +[Run a command on remote server and get STDOUT output](https://github.com/pnrmx/easyssh/blob/master/example/run.go) -[Upload a file to remote server](https://github.com/hypersleep/easyssh/blob/master/example/scp.go) +[Upload a file to remote server](https://github.com/pnrmx/easyssh/blob/master/example/scp.go) From 189656c0e263227e1b8cb4ecb8d2b00d1c267ba0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 15 Nov 2019 17:19:35 +0400 Subject: [PATCH 2/2] added key HostKeyCallback, due to go ssh changes --- easyssh.go | 46 ++++++++++++++++++++++++++++++++++++++-------- example/run.go | 1 + example/scp.go | 1 + 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/easyssh.go b/easyssh.go index c530eea..4c22aa0 100644 --- a/easyssh.go +++ b/easyssh.go @@ -27,17 +27,18 @@ import ( // Note: easyssh looking for private key in user's home directory (ex. /home/john + Key). // Then ensure your Key begins from '/' (ex. /.ssh/id_rsa) type MakeConfig struct { - User string - Server string - Key string - Port string - Password string + User string + Server string + Key string + Port string + Password string + FixedHost string } // returns ssh.Signer from user you running app home path + cutted key path. // (ex. pubkey,err := getKeyFile("/.ssh/id_rsa") ) func getKeyFile(keypath string) (ssh.Signer, error) { - usr, err := user.Current() + usr, err := user.Current() //returns user{uid,gid,username,name,homedir} if err != nil { return nil, err } @@ -56,6 +57,28 @@ func getKeyFile(keypath string) (ssh.Signer, error) { return pubkey, nil } +// ex. getHostKey ("/.ssh/known_hosts") +func getHostKey(hostpath string) (ssh.PublicKey, error) { + + usr, err := user.Current() //returns user{uid,gid,username,name,homedir} + if err != nil { + return nil, err + } + + file := usr.HomeDir + hostpath + buf, err := ioutil.ReadFile(file) + if err != nil { + return nil, err + } + + _, _, hostkey, _, _, err := ssh.ParseKnownHosts(buf) + if err != nil { + return nil, err + } + + return hostkey, nil +} + // connects to remote server using MakeConfig struct and returns *ssh.Session func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { // auths holds the detected ssh auth methods @@ -75,9 +98,16 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { auths = append(auths, ssh.PublicKeys(pubkey)) } + hostkey, err := getHostKey(ssh_conf.FixedHost) + if err != nil { + return nil, err + } + hostCallBack := ssh.FixedHostKey(hostkey) + config := &ssh.ClientConfig{ - User: ssh_conf.User, - Auth: auths, + User: ssh_conf.User, + Auth: auths, + HostKeyCallback: hostCallBack, } client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config) diff --git a/example/run.go b/example/run.go index c68152d..4ed5b63 100644 --- a/example/run.go +++ b/example/run.go @@ -14,6 +14,7 @@ func main() { // Optional key or Password without either we try to contact your agent SOCKET //Password: "password", Key: "/.ssh/id_rsa", + FixedHost: "/.ssh/known_hosts", Port: "22", } diff --git a/example/scp.go b/example/scp.go index 39d4bc3..95164d0 100644 --- a/example/scp.go +++ b/example/scp.go @@ -11,6 +11,7 @@ func main() { User: "root", Server: "example.com", Password: "123qwe", + FixedHost: "/.ssh/known_hosts", Port: "22", }