123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package remote
- import (
- "bytes"
- "errors"
- "os"
- "xg_auto_deploy/internal/global"
- "xg_auto_deploy/internal/models"
- "golang.org/x/crypto/ssh"
- )
- func SingleCmd(nodeConfig *models.ServerNodeConfig, cmd string) (string, error) {
- // 作为客户端连接SSH服务器
- client, err := ssh.Dial("tcp", nodeConfig.IpPort, nodeConfig.SSHClient)
- if err != nil {
- global.Logs.Fatal("ssh服务器失败 (Failed to dial): ", err)
- os.Exit(0)
- }
- defer client.Close()
- // 创建会话
- session, err := client.NewSession()
- if err != nil {
- global.Logs.Fatal("Failed to create session: ", err)
- os.Exit(0)
- }
- defer session.Close()
- // 设置会话标准输出,运行命令
- var b bytes.Buffer
- session.Stdout = &b
- //global.Log.Info("执行命令:", cmd)
- if err := session.Run(cmd); err != nil {
- // log.Errorf("Failed to run: " + cmd)
- // log.Errorf("命令运行失败: %s", err)
- return b.String(), errors.New("命令发送失败 (failed to run)")
- }
- //fmt.Println(b.String())
- return b.String(), nil
- }
- func SingleCmdTime(nodeConfig *models.ServerNodeConfig, cmd string) (string, error) {
- return "", nil
- }
|