123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package remote
- import (
- "bytes"
- "io"
- "xg_auto_deploy/internal/global"
- "xg_auto_deploy/internal/models"
- "github.com/pkg/sftp"
- "golang.org/x/crypto/ssh"
- )
- func DownloadFileBuffer(nodeConfig *models.ServerNodeConfig, targetFile string) *bytes.Buffer {
- // 1. 建立 ssh client
- client, err := ssh.Dial("tcp", nodeConfig.IpPort, nodeConfig.SSHClient)
- if err != nil {
- global.Logs.Fatal("DownloadFailed to dial: ", err)
- }
- defer client.Close()
- // 2. 基于ssh client, 创建 sftp 客户端
- sftpClient, err := sftp.NewClient(client)
- if err != nil {
- global.Logs.Fatal("Download Failed to init sftp client: ", err)
- }
- defer sftpClient.Close()
- // 3. 打开远程服务器文件
- //filename := time.Now().Format("2006-01-02") + ".global.Log"
- source, err := sftpClient.Open(targetFile)
- if err != nil {
- global.Logs.Fatal("Download Failed to open remote file: ", err)
- }
- defer source.Close()
- // 读取文件内容到内存
- buf := new(bytes.Buffer)
- _, err = io.Copy(buf, source)
- if err != nil {
- global.Logs.Fatal("Download Failed to read file: ", err)
- }
- //fmt.Println("buf: ", buf)
- return buf
- }
|