From e98562783e104879582520fa42d0f77cff85fcc2 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Sat, 16 Sep 2023 14:51:26 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=BF=9C=E7=A8=8B=E7=BD=91=E5=85=83scp?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- features/udm_user/api_udm_user.go | 28 ++++++++++++------------ lib/core/file/ssh.go | 36 ++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/features/udm_user/api_udm_user.go b/features/udm_user/api_udm_user.go index 4c5cb93f..cc96b39b 100644 --- a/features/udm_user/api_udm_user.go +++ b/features/udm_user/api_udm_user.go @@ -561,23 +561,23 @@ func (s *UdmUserApi) UdmAuthUserImport(w http.ResponseWriter, r *http.Request) { // 文件名 fileName := fmt.Sprintf("OMC_AUTH_USER_IMPORT_%s_%d_%s", neId, time.Now().UnixMilli(), fileHeader.Filename) - filePath := fmt.Sprintf("%s/upload/mml/%s", conf.Get("ne.omcdir"), fileName) - dstPath := conf.Get("mml.upload").(string) + localPath := fmt.Sprintf("%s/upload/mml/%s", conf.Get("ne.omcdir"), fileName) + nePath := conf.Get("mml.upload").(string) // 输出保存文件 - err = ctx.SaveUploadedFile(r, filePath) + err = ctx.SaveUploadedFile(r, localPath) if err != nil { ctx.JSON(w, 200, result.ErrMsg(err.Error())) return } // 复制到远程 - err = file.FileNeSCP(neInfo.Ip, filePath, dstPath) + err = file.FileSCPLocalToNe(neInfo.Ip, localPath, nePath) if err != nil { ctx.JSON(w, 200, result.ErrMsg(err.Error())) return } - msg := fmt.Sprintf("import authdat:path=%s", fmt.Sprintf("%s/%s", dstPath, fileName)) + msg := fmt.Sprintf("import authdat:path=%s", fmt.Sprintf("%s/%s", nePath, fileName)) // 发送MML data, err := mmlclient.MMLSendMsgToString(neInfo.Ip, msg) @@ -589,12 +589,12 @@ func (s *UdmUserApi) UdmAuthUserImport(w http.ResponseWriter, r *http.Request) { // 命令ok时 if strings.Contains(data, "ok") { if strings.HasSuffix(fileHeader.Filename, ".csv") { - data := file.ReadCSVFile(filePath) + data := file.ReadCSVFile(localPath) neId = "-" s.authUser.InsertCSV(neId, data) } if strings.HasSuffix(fileHeader.Filename, ".txt") { - data := file.ReadTxtFile(filePath) + data := file.ReadTxtFile(localPath) neId = "-" s.authUser.InsertTxt(neId, data) } @@ -1125,23 +1125,23 @@ func (s *UdmUserApi) UdmSubUserImport(w http.ResponseWriter, r *http.Request) { // 文件名 fileName := fmt.Sprintf("OMC_SUB_USER_IMPORT_%s_%d_%s", neId, time.Now().UnixMilli(), fileHeader.Filename) - filePath := fmt.Sprintf("%s/upload/mml/%s", conf.Get("ne.omcdir"), fileName) - dstPath := conf.Get("mml.upload").(string) + localPath := fmt.Sprintf("%s/upload/mml/%s", conf.Get("ne.omcdir"), fileName) + nePath := conf.Get("mml.upload").(string) // 输出保存文件 - err = ctx.SaveUploadedFile(r, filePath) + err = ctx.SaveUploadedFile(r, localPath) if err != nil { ctx.JSON(w, 200, result.ErrMsg(err.Error())) return } // 复制到远程 - err = file.FileNeSCP(neInfo.Ip, filePath, dstPath) + err = file.FileSCPLocalToNe(neInfo.Ip, localPath, nePath) if err != nil { ctx.JSON(w, 200, result.ErrMsg(err.Error())) return } - msg := fmt.Sprintf("import udmuser:path=%s", fmt.Sprintf("%s/%s", dstPath, fileName)) + msg := fmt.Sprintf("import udmuser:path=%s", fmt.Sprintf("%s/%s", nePath, fileName)) // 发送MML data, err := mmlclient.MMLSendMsgToString(neInfo.Ip, msg) @@ -1152,12 +1152,12 @@ func (s *UdmUserApi) UdmSubUserImport(w http.ResponseWriter, r *http.Request) { // 命令ok时 if strings.Contains(data, "ok") { if strings.HasSuffix(fileHeader.Filename, ".csv") { - data := file.ReadCSVFile(filePath) + data := file.ReadCSVFile(localPath) neId = "-" s.subUser.InsertCSV(neId, data) } if strings.HasSuffix(fileHeader.Filename, ".txt") { - data := file.ReadTxtFile(filePath) + data := file.ReadTxtFile(localPath) neId = "-" s.subUser.InsertTxt(neId, data) } diff --git a/lib/core/file/ssh.go b/lib/core/file/ssh.go index 07e4d7c2..c33a717a 100644 --- a/lib/core/file/ssh.go +++ b/lib/core/file/ssh.go @@ -2,22 +2,48 @@ package file import ( "fmt" + "os" "os/exec" + "path/filepath" "ems.agt/lib/core/conf" "ems.agt/lib/log" ) -// 网元NE 文件复制到远程文件夹 -func FileNeSCP(neIp, filePath, dstPath string) error { +// 网元NE 文件复制到远程文件 +func FileSCPLocalToNe(neIp, localPath, nePath string) error { usernameNe := conf.Get("ne.user").(string) // scp /path/to/local/file.txt user@remote-server:/path/to/remote/directory/ - dstDir := fmt.Sprintf("%s@%s:%s", usernameNe, neIp, dstPath) - cmd := exec.Command("scp", "-r", filePath, dstDir) + neDir := fmt.Sprintf("%s@%s:%s", usernameNe, neIp, nePath) + cmd := exec.Command("scp", "-r", localPath, neDir) out, err := cmd.CombinedOutput() if err != nil { return err } - log.Infof("FileNeSCP %s", string(out)) + log.Infof("FileSCPLocalToNe %s", string(out)) + return nil +} + +// 网元NE 远程文件复制到本地文件 +func FileSCPNeToLocal(neIp, nePath, localPath string) error { + // 获取文件所在的目录路径 + dirPath := filepath.Dir(localPath) + + // 确保文件夹路径存在 + err := os.MkdirAll(dirPath, os.ModePerm) + if err != nil { + log.Errorf("创建文件夹失败 CreateFile %v", err) + return err + } + + usernameNe := conf.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) + out, err := cmd.CombinedOutput() + if err != nil { + return err + } + log.Infof("FileSCPNeToLocal %s", string(out)) return nil }