From 77443979dea407e4b2d7920f624290f919c87700 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 26 Feb 2024 11:57:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ssh=E5=8C=85=E7=A7=BB=E9=99=A4=E5=86=99?= =?UTF-8?q?=E5=85=A5=E5=91=BD=E4=BB=A4=E5=9B=9E=E8=BD=A6=E8=A1=A5=E4=BD=8D?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E8=AF=BB=E5=8F=96=E7=AD=89=E5=BE=85?= =?UTF-8?q?300s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/ssh/ssh.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/framework/utils/ssh/ssh.go b/src/framework/utils/ssh/ssh.go index be00b088..c02dcfc9 100644 --- a/src/framework/utils/ssh/ssh.go +++ b/src/framework/utils/ssh/ssh.go @@ -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()