修正 MkdirAll 算法,当文件存在时不报错.
Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
parent
5aaea5918d
commit
f7718fbf01
|
@ -10,3 +10,4 @@
|
|||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
go.sum
|
||||
|
|
8
auth.go
8
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
|
||||
|
|
|
@ -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
|
||||
)
|
20
sftp.go
20
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 && 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 {
|
||||
|
|
72
sftp_test.go
72
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 {
|
||||
|
|
3
ssh.go
3
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue