fix: ssh包移除写入命令回车补位,移除读取等待300s

This commit is contained in:
TsMask
2024-02-26 11:57:57 +08:00
parent d872e0d5b4
commit 77443979de

View File

@@ -2,31 +2,15 @@ package ssh
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"time"
"ems.agt/src/framework/logger"
gossh "golang.org/x/crypto/ssh"
)
// CopyHost 复制网元主机信息josn同key名
func CopyHost(to, from interface{}) error {
b, err := json.Marshal(from)
if err != nil {
logger.Errorf("CopyHost Marshal from data err %s", err.Error())
return err
}
if err = json.Unmarshal(b, to); err != nil {
logger.Errorf("CopyHost Unmarshal to data err %s", err.Error())
return err
}
return nil
}
// ConnSSH 连接SSH对象
type ConnSSH struct {
User string `json:"user"` // 主机用户名
@@ -172,23 +156,20 @@ func (s *SSHClientSession) Close() {
}
}
// Write 写入命令
// Write 写入命令 回车(\n)才会执行
func (s *SSHClientSession) Write(cmd string) (int, error) {
if s.Stdin == nil {
return 0, fmt.Errorf("stdin is nil to content write failed")
}
if strings.LastIndexByte(cmd, '\n') == -1 {
cmd = fmt.Sprintln(cmd)
return 0, fmt.Errorf("ssh client session is nil to content write failed")
}
return s.Stdin.Write([]byte(cmd))
}
// Read 读取结果
// Read 读取结果 等待一会才有结果
func (s *SSHClientSession) Read() []byte {
if s.Stdout == nil {
return []byte{}
}
time.Sleep(300 * time.Millisecond)
// time.Sleep(300 * time.Millisecond)
bs := s.Stdout.Bytes()
if len(bs) > 0 {
s.Stdout.Reset()