fix: 网元操作文件列表/上传/下载改为ssh连接
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/config"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/cmd"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
@@ -23,11 +22,10 @@ type FileListRow struct {
|
||||
}
|
||||
|
||||
// 文件列表
|
||||
// neIp 网元IP空字符串为本地
|
||||
// search 文件名后模糊*
|
||||
//
|
||||
// return 目录大小,行记录,异常
|
||||
func FileList(path, neIp, search string) (string, []FileListRow, error) {
|
||||
func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, error) {
|
||||
totalSize := ""
|
||||
var rows []FileListRow
|
||||
rowStr := ""
|
||||
@@ -37,21 +35,18 @@ func FileList(path, neIp, search string) (string, []FileListRow, error) {
|
||||
if search != "" {
|
||||
searchStr = search + searchStr
|
||||
}
|
||||
pathStr := fmt.Sprintf("cd %s \n", path)
|
||||
cmdStr := fmt.Sprintf("ls -lthd --time-style=+%%s %s \n", searchStr)
|
||||
cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr)
|
||||
|
||||
// 是否远程读取
|
||||
if neIp != "" {
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neIp)
|
||||
resultStr, err := cmd.ExecWithCheck("ssh", sshHost, pathStr, cmdStr)
|
||||
// 是否远程客户端读取
|
||||
if sshClient == nil {
|
||||
resultStr, err := cmd.Execf(cmdStr)
|
||||
if err != nil {
|
||||
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
|
||||
return totalSize, rows, err
|
||||
}
|
||||
rowStr = resultStr
|
||||
} else {
|
||||
resultStr, err := cmd.Execf(pathStr, cmdStr)
|
||||
resultStr, err := sshClient.RunCMD(cmdStr)
|
||||
if err != nil {
|
||||
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
|
||||
return totalSize, rows, err
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
@@ -50,19 +51,30 @@ func (s *NeActionController) PushFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 本地文件
|
||||
localPath := file.ParseUploadFilePath(body.UploadPath)
|
||||
nePath := "/tmp" //config.Get("mml.upload").(string)
|
||||
// 复制到远程
|
||||
err := ssh.FileSCPLocalToNe(neInfo.IP, localPath, nePath)
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := s.neInfoService.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
// 本地文件
|
||||
localFilePath := file.ParseUploadFilePath(body.UploadPath)
|
||||
neFilePath := fmt.Sprintf("/tmp/%s", filepath.Base(localFilePath))
|
||||
// 复制到远程
|
||||
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
|
||||
c.JSON(200, result.ErrMsg(fmt.Sprintf("%s : please check if scp remote copy is allowed", neInfo.NeType)))
|
||||
return
|
||||
}
|
||||
|
||||
// 网元端文件路径
|
||||
fileName := localPath[strings.LastIndex(localPath, "/")+1:]
|
||||
neFilePath := fmt.Sprintf("%s/%s", nePath, fileName)
|
||||
c.JSON(200, result.OkData(filepath.ToSlash(neFilePath)))
|
||||
}
|
||||
|
||||
@@ -89,14 +101,32 @@ func (s *NeActionController) PullFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
nePath := fmt.Sprintf("%s/%s", querys.Path, querys.FileName)
|
||||
localPath := fmt.Sprintf("/tmp/omc/pullFile/%s", querys.FileName)
|
||||
err := ssh.FileSCPNeToLocal(neInfo.IP, nePath, localPath)
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := s.neInfoService.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.FileAttachment(localPath, querys.FileName)
|
||||
defer sshClient.Close()
|
||||
// 网元主机的SSH客户端进行文件传输
|
||||
sftpClient, err := sshClient.NewClientSFTP()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
nePath := fmt.Sprintf("%s/%s", querys.Path, querys.FileName)
|
||||
localFilePath := fmt.Sprintf("/tmp/omc/pullFile%s", nePath)
|
||||
if runtime.GOOS == "windows" {
|
||||
localFilePath = fmt.Sprintf("C:%s", localFilePath)
|
||||
}
|
||||
// 复制到本地
|
||||
if err = sftpClient.CopyFileRemoteToLocal(nePath, localFilePath); err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.FileAttachment(localFilePath, querys.FileName)
|
||||
}
|
||||
|
||||
// 网元端文件列表
|
||||
@@ -124,7 +154,16 @@ func (s *NeActionController) Files(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
totalSize, rows, err := ssh.FileList(querys.Path, neInfo.IP, querys.Search)
|
||||
// 网元主机的SSH客户端
|
||||
sshClient, err := s.neInfoService.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
defer sshClient.Close()
|
||||
|
||||
// 获取文件列表
|
||||
totalSize, rows, err := ssh.FileList(sshClient, querys.Path, querys.Search)
|
||||
if err != nil {
|
||||
c.JSON(200, result.Ok(map[string]any{
|
||||
"path": querys.Path,
|
||||
|
||||
Reference in New Issue
Block a user