From 0808f313f5c45a701020496ba39f74e2b677ab18 Mon Sep 17 00:00:00 2001 From: Aram Date: Sat, 16 Jul 2016 17:39:17 +0400 Subject: [PATCH 1/2] Download remote file functionality has been added --- easyssh.go | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/easyssh.go b/easyssh.go index b3002d5..895ac8c 100644 --- a/easyssh.go +++ b/easyssh.go @@ -16,6 +16,7 @@ import ( "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" + "github.com/pkg/sftp" ) // Contains main authority information. @@ -56,7 +57,7 @@ func getKeyFile(keypath string) (ssh.Signer, error) { } // connects to remote server using MakeConfig struct and returns *ssh.Session -func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { +func (ssh_conf *MakeConfig) connect() (*ssh.Client, *ssh.Session, error) { // auths holds the detected ssh auth methods auths := []ssh.AuthMethod{} @@ -81,15 +82,15 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config) if err != nil { - return nil, err + return nil, nil, err } session, err := client.NewSession() if err != nil { - return nil, err + return nil, nil, err } - return session, nil + return client, session, nil } // Stream returns one channel that combines the stdout and stderr of the command @@ -97,7 +98,7 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { // command is done. The sessions and channels will then be closed. func (ssh_conf *MakeConfig) Stream(command string) (output chan string, done chan bool, err error) { // connect to remote host - session, err := ssh_conf.connect() + _, session, err := ssh_conf.connect() if err != nil { return output, done, err } @@ -152,7 +153,7 @@ func (ssh_conf *MakeConfig) Run(command string) (outStr string, err error) { // Scp uploads sourceFile to remote machine like native scp console app. func (ssh_conf *MakeConfig) Scp(sourceFile string) error { - session, err := ssh_conf.connect() + _, session, err := ssh_conf.connect() if err != nil { return err @@ -194,3 +195,33 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string) error { return nil } + +func (ssh_conf *MakeConfig) Download(sourceFile string, destinationFile string) error { + client, _, err := ssh_conf.connect() + + // open an SFTP session over an existing ssh connection. + sftp, err := sftp.NewClient(client) + if err != nil { + return err + } + defer sftp.Close() + + // Open the source file + srcFile, err := sftp.Open(sourceFile) + if err != nil { + return err + } + defer srcFile.Close() + + // Create the destination file + dstFile, err := os.Create(destinationFile) + if err != nil { + return err + } + defer dstFile.Close() + + // Copy the file + srcFile.WriteTo(dstFile) + + return err +} From ff76c7d8e08066ac6ae45810938d4919d369d6db Mon Sep 17 00:00:00 2001 From: Aram Date: Sat, 16 Jul 2016 22:50:39 +0400 Subject: [PATCH 2/2] Upload local file functionality has been added --- easyssh.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/easyssh.go b/easyssh.go index 895ac8c..8ccd47b 100644 --- a/easyssh.go +++ b/easyssh.go @@ -152,7 +152,7 @@ func (ssh_conf *MakeConfig) Run(command string) (outStr string, err error) { } // Scp uploads sourceFile to remote machine like native scp console app. -func (ssh_conf *MakeConfig) Scp(sourceFile string) error { +func (ssh_conf *MakeConfig) Upload(sourceFile string, destinationDir string) error { _, session, err := ssh_conf.connect() if err != nil { @@ -189,7 +189,7 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string) error { } }() - if err := session.Run(fmt.Sprintf("scp -t %s", targetFile)); err != nil { + if err := session.Run(fmt.Sprintf("cd %s && scp -t %s", destinationDir, targetFile)); err != nil { return err } @@ -197,7 +197,12 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string) error { } func (ssh_conf *MakeConfig) Download(sourceFile string, destinationFile string) error { - client, _, err := ssh_conf.connect() + client, session, err := ssh_conf.connect() + + if err != nil { + return err + } + defer session.Close() // open an SFTP session over an existing ssh connection. sftp, err := sftp.NewClient(client)