feat: 移除ssh客户端改为本地bash

This commit is contained in:
TsMask
2025-05-13 17:22:04 +08:00
parent 552abb411c
commit f7f63f1950
21 changed files with 526 additions and 590 deletions

View File

@@ -9,10 +9,10 @@ import (
"github.com/gin-gonic/gin"
"be.ems/src/framework/cmd"
"be.ems/src/framework/i18n"
"be.ems/src/framework/reqctx"
"be.ems/src/framework/resp"
"be.ems/src/framework/ssh"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/generate"
neService "be.ems/src/modules/network_element/service"
@@ -66,27 +66,32 @@ func (s *NeActionController) PushFile(c *gin.Context) {
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sshClient.Close()
// // 网元主机的SSH客户端进行文件传输
// sftpClient, err := sshClient.NewClientSFTP()
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sftpClient.Close()
// 本地文件
localFilePath := file.ParseUploadFileAbsPath(body.UploadPath)
// 网元端临时目录
sshClient.RunCMD("mkdir -p /tmp/omc/push && sudo chmod 777 -R /tmp/omc")
// sshClient.RunCMD("mkdir -p /tmp/omc/push && sudo chmod 777 -R /tmp/omc")
cmd.Exec("mkdir -p /tmp/omc/push && sudo chmod 777 -R /tmp/omc")
neFilePath := filepath.ToSlash(filepath.Join("/tmp/omc/push", filepath.Base(localFilePath)))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
// if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
// c.JSON(200, resp.ErrMsg("Please check if the file exists or if scp is allowed to copy remotely"))
// return
// }
if err := file.CopyFile(localFilePath, neFilePath); err != nil {
c.JSON(200, resp.ErrMsg("Please check if the file exists or if scp is allowed to copy remotely"))
return
}
@@ -139,19 +144,19 @@ func (s *NeActionController) PullFile(c *gin.Context) {
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sshClient.Close()
// // 网元主机的SSH客户端进行文件传输
// sftpClient, err := sshClient.NewClientSFTP()
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sftpClient.Close()
nePath := filepath.ToSlash(filepath.Join(querys.Path, querys.FileName))
fileName := generate.Code(6) + "_" + querys.FileName
@@ -160,7 +165,11 @@ func (s *NeActionController) PullFile(c *gin.Context) {
localFilePath = fmt.Sprintf("C:%s", localFilePath)
}
// 复制到本地
if err = sftpClient.CopyFileRemoteToLocal(nePath, localFilePath); err != nil {
// if err = sftpClient.CopyFileRemoteToLocal(nePath, localFilePath); err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
if err := file.CopyFile(nePath, localFilePath); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
@@ -211,19 +220,19 @@ func (s *NeActionController) PullDirZip(c *gin.Context) {
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sshClient.Close()
// // 网元主机的SSH客户端进行文件传输
// sftpClient, err := sshClient.NewClientSFTP()
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sftpClient.Close()
nePath := querys.Path
dirName := generate.Code(6)
@@ -233,7 +242,11 @@ func (s *NeActionController) PullDirZip(c *gin.Context) {
}
// 复制到本地
localDirFilePath := filepath.Join(localFilePath, "zip")
if err = sftpClient.CopyDirRemoteToLocal(nePath, localDirFilePath); err != nil {
// if err = sftpClient.CopyDirRemoteToLocal(nePath, localDirFilePath); err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
if err := file.CopyDir(nePath, localDirFilePath); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
@@ -292,17 +305,17 @@ func (s *NeActionController) ViewFile(c *gin.Context) {
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sshClient.Close()
// 网元端文件
nePath := filepath.ToSlash(filepath.Join(querys.Path, querys.FileName))
// 网元端临时目录
output, err := sshClient.RunCMD(fmt.Sprintf("cat %s", nePath))
output, err := cmd.Execf("cat %s", nePath)
output = strings.TrimSpace(output)
if err != nil || strings.HasPrefix(output, "ls: ") {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "file view cat error")))
@@ -330,7 +343,6 @@ func (s *NeActionController) ViewFile(c *gin.Context) {
// @Description List of files on the network element side
// @Router /ne/action/files [get]
func (s *NeActionController) Files(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
NeID string `form:"neId" binding:"required"`
@@ -345,28 +357,28 @@ func (s *NeActionController) Files(c *gin.Context) {
return
}
// 查询网元获取IP
neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// // 查询网元获取IP
// neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
// if neInfo.NeId != querys.NeID || neInfo.IP == "" {
// c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
// return
// }
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// // 网元主机的SSH客户端
// sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
// if err != nil {
// c.JSON(200, resp.ErrMsg(err.Error()))
// return
// }
// defer sshClient.Close()
// 获取文件列表
rows, err := ssh.FileList(sshClient, querys.Path, querys.Search)
rows, err := file.FileList(querys.Path, querys.Search)
if err != nil {
c.JSON(200, resp.OkData(map[string]any{
"path": querys.Path,
"total": len(rows),
"rows": []ssh.FileListRow{},
"rows": []file.FileListRow{},
}))
return
}
@@ -375,9 +387,9 @@ func (s *NeActionController) Files(c *gin.Context) {
lenNum := int64(len(rows))
start := (querys.PageNum - 1) * querys.PageSize
end := start + querys.PageSize
var splitRows []ssh.FileListRow
var splitRows []file.FileListRow
if start >= lenNum {
splitRows = []ssh.FileListRow{}
splitRows = []file.FileListRow{}
} else if end >= lenNum {
splitRows = rows[start:]
} else {