fix: ssh包移除写入命令回车补位,移除读取等待300s
This commit is contained in:
@@ -2,31 +2,15 @@ package ssh
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"ems.agt/src/framework/logger"
|
|
||||||
gossh "golang.org/x/crypto/ssh"
|
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对象
|
// ConnSSH 连接SSH对象
|
||||||
type ConnSSH struct {
|
type ConnSSH struct {
|
||||||
User string `json:"user"` // 主机用户名
|
User string `json:"user"` // 主机用户名
|
||||||
@@ -172,23 +156,20 @@ func (s *SSHClientSession) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write 写入命令
|
// Write 写入命令 回车(\n)才会执行
|
||||||
func (s *SSHClientSession) Write(cmd string) (int, error) {
|
func (s *SSHClientSession) Write(cmd string) (int, error) {
|
||||||
if s.Stdin == nil {
|
if s.Stdin == nil {
|
||||||
return 0, fmt.Errorf("stdin is nil to content write failed")
|
return 0, fmt.Errorf("ssh client session is nil to content write failed")
|
||||||
}
|
|
||||||
if strings.LastIndexByte(cmd, '\n') == -1 {
|
|
||||||
cmd = fmt.Sprintln(cmd)
|
|
||||||
}
|
}
|
||||||
return s.Stdin.Write([]byte(cmd))
|
return s.Stdin.Write([]byte(cmd))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read 读取结果
|
// Read 读取结果 等待一会才有结果
|
||||||
func (s *SSHClientSession) Read() []byte {
|
func (s *SSHClientSession) Read() []byte {
|
||||||
if s.Stdout == nil {
|
if s.Stdout == nil {
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
time.Sleep(300 * time.Millisecond)
|
// time.Sleep(300 * time.Millisecond)
|
||||||
bs := s.Stdout.Bytes()
|
bs := s.Stdout.Bytes()
|
||||||
if len(bs) > 0 {
|
if len(bs) > 0 {
|
||||||
s.Stdout.Reset()
|
s.Stdout.Reset()
|
||||||
|
|||||||
Reference in New Issue
Block a user