From f7718fbf01ba6f06aeeeb7375c9d78b1bf2c0c09 Mon Sep 17 00:00:00 2001 From: "chen.yang" Date: Fri, 25 Feb 2022 11:10:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20MkdirAll=20=E7=AE=97?= =?UTF-8?q?=E6=B3=95,=E5=BD=93=E6=96=87=E4=BB=B6=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E6=8A=A5=E9=94=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen.yang --- .gitignore | 1 + auth.go | 8 +++--- go.mod | 18 +++++++++++++ sftp.go | 24 ++++++++---------- sftp_test.go | 72 ++++++++++++++++++++++++++-------------------------- ssh.go | 3 +-- 6 files changed, 71 insertions(+), 55 deletions(-) create mode 100644 go.mod diff --git a/.gitignore b/.gitignore index f1c181e..6479f25 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +go.sum diff --git a/auth.go b/auth.go index 1debd2a..4ef2ed6 100644 --- a/auth.go +++ b/auth.go @@ -11,7 +11,7 @@ import ( "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" - "golang.org/x/crypto/ssh/terminal" + "golang.org/x/term" ) //HasAgent reports whether the SSH agent is available @@ -67,7 +67,7 @@ func AuthWithAgent() (ssh.AuthMethod, error) { sock := os.Getenv("SSH_AUTH_SOCK") if sock == "" { // fmt.Println(errors.New("Agent Disabled")) - return nil, errors.New("Agent Disabled") + return nil, errors.New("AgentDisabled") } socks, err := net.Dial("unix", sock) if err != nil { @@ -76,7 +76,7 @@ func AuthWithAgent() (ssh.AuthMethod, error) { } // 1. 返回Signers函数的结果 agent := agent.NewClient(socks) - signers, err := agent.Signers() + signers, _ := agent.Signers() return ssh.PublicKeys(signers...), nil // 2. 返回Signers函数 // getSigners := agent.NewClient(socks).Signers @@ -172,7 +172,7 @@ func AuthWithPrivateKeyTerminal(keyfile string) (ssh.AuthMethod, error) { if strings.Contains(err.Error(), "cannot decode encrypted private keys") { fmt.Fprintf(os.Stderr, "This SSH key is encrypted. Please enter passphrase for key '%s':", keyfile) - passphrase, err := terminal.ReadPassword(int(syscall.Stdin)) + passphrase, err := term.ReadPassword(int(syscall.Stdin)) if err != nil { // println(err.Error()) return nil, err diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1cfd38f --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module blacktea.vip.cpolar.top/OrgGo/ssh + +go 1.17 + +require ( + github.com/go-sql-driver/mysql v1.6.0 + github.com/mitchellh/go-homedir v1.1.0 + github.com/pkg/sftp v1.13.4 + github.com/pytool/ssh v0.0.0-20190312091242-5aaea5918db7 + github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb + golang.org/x/crypto v0.0.0-20220214200702-86341886e292 + golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 +) + +require ( + github.com/kr/fs v0.1.0 // indirect + golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect +) diff --git a/sftp.go b/sftp.go index 8ebaab8..d4910db 100644 --- a/sftp.go +++ b/sftp.go @@ -60,7 +60,7 @@ func (c *Client) downloadFile(remoteFile, local string) error { localFile = filepath.ToSlash(localFile) if c.Size(remoteFile) > 1000 { rsum := c.Md5File(remoteFile) - ioutil.WriteFile(localFile+".md5", []byte(rsum), 755) + ioutil.WriteFile(localFile+".md5", []byte(rsum), 0755) if FileExist(localFile) { if rsum != "" { lsum, _ := Md5File(localFile) @@ -261,7 +261,6 @@ func (c *Client) UploadDir(localDir string, remoteDir string) (err error) { // it should exist and we might not even own it if finalDst == remoteDir { return nil - log.Println("sftp: ", remoteDir, "--->", finalDst) } @@ -354,25 +353,24 @@ func (c *Client) RemoveAll(remoteDir string) error { //MkdirAll 创建目录,递归 func (c *Client) MkdirAll(dirpath string) error { - - parentDir := filepath.ToSlash(filepath.Dir(dirpath)) - _, err := c.SFTPClient.Stat(parentDir) + _, err := c.SFTPClient.Stat(dirpath) if err != nil { - // log.Println(err) if err.Error() == "file does not exist" { - err := c.MkdirAll(parentDir) - if err != nil { - return err + e := c.MkdirAll(filepath.ToSlash(filepath.Dir(dirpath))) + if e != nil { + return e } } else { return err } } - err = c.SFTPClient.Mkdir(filepath.ToSlash(dirpath)) - if err != nil { - return err + if err != nil && err.Error() == "file does not exist" { + err = c.SFTPClient.Mkdir(dirpath) + if err != nil { + return err + } } - return nil + return err } func (c *Client) Mkdir(path string, fi os.FileInfo) error { diff --git a/sftp_test.go b/sftp_test.go index d5e8ee7..8558d3b 100644 --- a/sftp_test.go +++ b/sftp_test.go @@ -45,12 +45,12 @@ func TestClient_Upload(t *testing.T) { var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/" var uploads = map[string][]string{ - local + "null/": []string{"/root/upload/test/null/1", "/root/upload/test/null/2/"}, - local + "null/": []string{"/root/upload/test/null/3", "/root/upload/test/null/4/"}, - local + "file": []string{"/root/upload/test/file/1", "/root/upload/test/file/2/"}, - local + "file/": []string{"/root/upload/test/file/3", "/root/upload/test/file/4/"}, - local + "dir": []string{"/root/upload/test/dir/1", "/root/upload/test/dir/2/"}, - local + "dir/": []string{"/root/upload/test/dir/3", "/root/upload/test/dir/4/"}, + local + "null/": {"/root/upload/test/null/1", "/root/upload/test/null/2/"}, + local + "null/": {"/root/upload/test/null/3", "/root/upload/test/null/4/"}, + local + "file": {"/root/upload/test/file/1", "/root/upload/test/file/2/"}, + local + "file/": {"/root/upload/test/file/3", "/root/upload/test/file/4/"}, + local + "dir": {"/root/upload/test/dir/1", "/root/upload/test/dir/2/"}, + local + "dir/": {"/root/upload/test/dir/3", "/root/upload/test/dir/4/"}, } for local, remotes := range uploads { @@ -71,12 +71,12 @@ func TestClient_Download(t *testing.T) { var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/download" var download = map[string][]string{ - "/root/test/notExist": []string{local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, - "/root/test/notExist/": []string{local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, - "/root/test/file": []string{local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, - "/root/test/file/": []string{local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, - "/root/test/dir": []string{local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, - "/root/test/dir/": []string{local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, + "/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, + "/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, + "/root/test/file": {local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, + "/root/test/file/": {local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, + "/root/test/dir": {local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, + "/root/test/dir/": {local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, } for remote, local := range download { @@ -98,12 +98,12 @@ func TestClient_DownloadFile(t *testing.T) { var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/downloadfile" var download = map[string][]string{ - "/root/test/notExist": []string{local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, - "/root/test/notExist/": []string{local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, - "/root/test/file": []string{local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, - "/root/test/file/": []string{local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, - "/root/test/dir": []string{local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, - "/root/test/dir/": []string{local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, + "/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, + "/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, + "/root/test/file": {local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, + "/root/test/file/": {local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, + "/root/test/dir": {local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, + "/root/test/dir/": {local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, } for remote, local := range download { @@ -123,12 +123,12 @@ func TestClient_DownloadDir(t *testing.T) { var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/downloaddir" var download = map[string][]string{ - "/root/test/notExist": []string{local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, - "/root/test/notExist/": []string{local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, - "/root/test/file": []string{local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, - "/root/test/file/": []string{local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, - "/root/test/dir": []string{local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, - "/root/test/dir/": []string{local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, + "/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"}, + "/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"}, + "/root/test/file": {local + "/localNotExist/file/1", local + "/localNotExist/file/2/"}, + "/root/test/file/": {local + "/localNotExist/file/3", local + "/localNotExist/file/4/"}, + "/root/test/dir": {local + "/localNotExist/dir/1", local + "/localNotExist/dir/2/"}, + "/root/test/dir/": {local + "/localNotExist/dir/3", local + "/localNotExist/dir/4/"}, } for remote, local := range download { @@ -147,12 +147,12 @@ func TestClient_UploadFile(t *testing.T) { defer c.Close() var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/" var uploads = map[string][]string{ - local + "null": []string{"/root/upload/file_test/null/1", "/root/upload/file_test/null/2/"}, - local + "null/": []string{"/root/upload/file_test/null/3", "/root/upload/file_test/null/4/"}, - local + "file": []string{"/root/upload/file_test/file/1", "/root/upload/file_test/file/2/"}, - local + "file/": []string{"/root/upload/file_test/file/3", "/root/upload/file_test/file/4/"}, - local + "dir": []string{"/root/upload/file_test/dir/1", "/root/upload/file_test/dir/2/"}, - local + "dir/": []string{"/root/upload/file_test/dir/3", "/root/upload/file_test/dir/4/"}, + local + "null": {"/root/upload/file_test/null/1", "/root/upload/file_test/null/2/"}, + local + "null/": {"/root/upload/file_test/null/3", "/root/upload/file_test/null/4/"}, + local + "file": {"/root/upload/file_test/file/1", "/root/upload/file_test/file/2/"}, + local + "file/": {"/root/upload/file_test/file/3", "/root/upload/file_test/file/4/"}, + local + "dir": {"/root/upload/file_test/dir/1", "/root/upload/file_test/dir/2/"}, + local + "dir/": {"/root/upload/file_test/dir/3", "/root/upload/file_test/dir/4/"}, } for local, remotes := range uploads { @@ -172,12 +172,12 @@ func TestClient_UploadDir(t *testing.T) { defer c.Close() var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/" var uploads = map[string][]string{ - local + "null/": []string{"/root/upload/dir_test/null/1", "/root/upload/dir_test/null/2/"}, - local + "null/": []string{"/root/upload/dir_test/null/3", "/root/upload/dir_test/null/4/"}, - local + "file": []string{"/root/upload/dir_test/file/1", "/root/upload/dir_test/file/2/"}, - local + "file/": []string{"/root/upload/dir_test/file/3", "/root/upload/dir_test/file/4/"}, - local + "dir": []string{"/root/upload/dir_test/dir/1", "/root/upload/dir_test/dir/2/"}, - local + "dir/": []string{"/root/upload/dir_test/dir/3", "/root/upload/dir_test/dir/4/"}, + local + "null/": {"/root/upload/dir_test/null/1", "/root/upload/dir_test/null/2/"}, + local + "null/": {"/root/upload/dir_test/null/3", "/root/upload/dir_test/null/4/"}, + local + "file": {"/root/upload/dir_test/file/1", "/root/upload/dir_test/file/2/"}, + local + "file/": {"/root/upload/dir_test/file/3", "/root/upload/dir_test/file/4/"}, + local + "dir": {"/root/upload/dir_test/dir/1", "/root/upload/dir_test/dir/2/"}, + local + "dir/": {"/root/upload/dir_test/dir/3", "/root/upload/dir_test/dir/4/"}, } for local, remotes := range uploads { diff --git a/ssh.go b/ssh.go index bb82879..760dab0 100644 --- a/ssh.go +++ b/ssh.go @@ -28,7 +28,6 @@ func (c *Client) Run(cmd string) { if err != nil { fmt.Printf(":Command finished with error: %v\n", err) } - return } //Exec Execute cmd on the remote host and bind stderr and stdout @@ -48,7 +47,7 @@ func (c *Client) Exec1(cmd string) error { session.Stdout = os.Stdout session.Stderr = os.Stderr - err = session.Run(cmd) + session.Run(cmd) session.Close() return nil