fix 远程网元scp复制文件

This commit is contained in:
TsMask
2023-09-16 14:51:26 +08:00
parent 4cc49bc7b5
commit e98562783e
2 changed files with 45 additions and 19 deletions

View File

@@ -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
}