From fed2c577004a487f608fd20f3a79923a4c7a246b Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 1 Apr 2024 17:03:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20scp=E7=BD=91=E5=85=83=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E5=88=B0=E6=9C=AC=E5=9C=B0=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=8E=9F=E5=85=88=E5=90=8C=E5=90=8D=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/ssh/scp.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/framework/utils/ssh/scp.go b/src/framework/utils/ssh/scp.go index 834ba8ed..8151b3a4 100644 --- a/src/framework/utils/ssh/scp.go +++ b/src/framework/utils/ssh/scp.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "time" "be.ems/src/framework/config" "be.ems/src/framework/logger" @@ -16,9 +17,9 @@ func FileSCPLocalToNe(neIp, localPath, nePath string) error { // scp /path/to/local/file.txt user@remote-server:/path/to/remote/directory/ neDir := fmt.Sprintf("%s@%s:%s", usernameNe, neIp, nePath) cmd := exec.Command("scp", "-r", localPath, neDir) - _, err := cmd.CombinedOutput() + output, err := cmd.CombinedOutput() if err != nil { - logger.Errorf("FileSCPLocalToNe %s", err.Error()) + logger.Errorf("FileSCPLocalToNe %s => %s", output, err.Error()) return err } return nil @@ -31,13 +32,23 @@ func FileSCPNeToLocal(neIp, nePath, localPath string) error { logger.Errorf("FileSCPNeToLocal MkdirAll err %v", err) return err } + // 如果目标文件已经存在,先将目标文件重命名 + if info, err := os.Stat(localPath); err == nil && !info.IsDir() { + ext := filepath.Ext(localPath) + name := localPath[0 : len(localPath)-len(ext)] + newName := fmt.Sprintf("%s-%s%s", name, time.Now().Format("20060102_150405"), ext) + err := os.Rename(localPath, newName) + if err != nil { + return err + } + } usernameNe := config.Get("ne.user").(string) // scp user@remote-server:/path/to/remote/directory/ /path/to/local/file.txt neDir := fmt.Sprintf("%s@%s:%s", usernameNe, neIp, nePath) cmd := exec.Command("scp", "-r", neDir, localPath) - _, err := cmd.CombinedOutput() + output, err := cmd.CombinedOutput() if err != nil { - logger.Errorf("FileSCPNeToLocal %s", err.Error()) + logger.Errorf("FileSCPNeToLocal %s => %s", output, err.Error()) return err } return nil