style: 调整telnet工具包代码分割
This commit is contained in:
78
src/framework/utils/telnet/parse.go
Normal file
78
src/framework/utils/telnet/parse.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package telnet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConvertToStr 转换为string
|
||||||
|
func ConvertToStr(telnetClient *ConnTelnet, cmd string) (string, error) {
|
||||||
|
fmt.Println(cmd)
|
||||||
|
output, err := telnetClient.RunCMD(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
str := strings.ToLower(output)
|
||||||
|
|
||||||
|
// 截断
|
||||||
|
index := strings.Index(str, "\n")
|
||||||
|
if index != -1 {
|
||||||
|
str = str[:index]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 命令成功
|
||||||
|
if strings.Contains(str, "ok") || strings.Contains(str, "success") {
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvertToMap 转换为map
|
||||||
|
func ConvertToMap(telnetClient *ConnTelnet, cmd string) (map[string]string, error) {
|
||||||
|
output, err := telnetClient.RunCMD(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 无数据
|
||||||
|
if strings.HasPrefix(output, "No ") {
|
||||||
|
// 截断
|
||||||
|
index := strings.Index(output, "\n")
|
||||||
|
if index != -1 {
|
||||||
|
output = output[:index]
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化一个map用于存储拆分后的键值对
|
||||||
|
m := make(map[string]string)
|
||||||
|
|
||||||
|
var items []string
|
||||||
|
if strings.Contains(output, "\r\n") {
|
||||||
|
// 按照分隔符"\r\n"进行拆分
|
||||||
|
items = strings.Split(output, "\r\n")
|
||||||
|
} else if strings.Contains(output, "\n") {
|
||||||
|
// 按照分隔符"\n"进行拆分
|
||||||
|
items = strings.Split(output, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历拆分后的结果
|
||||||
|
for _, item := range items {
|
||||||
|
var pair []string
|
||||||
|
|
||||||
|
if strings.Contains(item, "=") {
|
||||||
|
// 按照分隔符"="进行拆分键值对
|
||||||
|
pair = strings.SplitN(item, "=", 2)
|
||||||
|
} else if strings.Contains(item, ":") {
|
||||||
|
// 按照分隔符":"进行拆分键值对
|
||||||
|
pair = strings.SplitN(item, ":", 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pair) == 2 {
|
||||||
|
// 将键值对存入map中
|
||||||
|
m[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
@@ -52,7 +52,7 @@ func (c *ConnTelnet) NewClient() (*ConnTelnet, error) {
|
|||||||
|
|
||||||
// 需要确保接收方理解并正确处理发送窗口大小设置命令
|
// 需要确保接收方理解并正确处理发送窗口大小设置命令
|
||||||
client.Write([]byte{255, 251, 31}) // 发送窗口大小选项
|
client.Write([]byte{255, 251, 31}) // 发送窗口大小选项
|
||||||
client.Write([]byte{255, 250, 31, 0, 128, 0, 120, 255, 240}) // 发送窗口行和列的大小
|
client.Write([]byte{255, 250, 31, 0, 120, 0, 128, 255, 240}) // 发送窗口行和列的大小 (120 列 x 128 行)
|
||||||
c.Client = &client
|
c.Client = &client
|
||||||
|
|
||||||
// 排空连接登录的信息
|
// 排空连接登录的信息
|
||||||
@@ -73,8 +73,6 @@ func (c *ConnTelnet) RunCMD(cmd string) (string, error) {
|
|||||||
return "", fmt.Errorf("telnet client not connected")
|
return "", fmt.Errorf("telnet client not connected")
|
||||||
}
|
}
|
||||||
conn := *c.Client
|
conn := *c.Client
|
||||||
var buf bytes.Buffer
|
|
||||||
tmp := make([]byte, 1024)
|
|
||||||
|
|
||||||
// 写入命令
|
// 写入命令
|
||||||
if cmd != "" {
|
if cmd != "" {
|
||||||
@@ -83,26 +81,29 @@ func (c *ConnTelnet) RunCMD(cmd string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取命令消息
|
var buf bytes.Buffer
|
||||||
|
tmp := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
// 设置读取超时时间为1000毫秒
|
// 读取命令消息
|
||||||
conn.SetReadDeadline(time.Now().Add(1000 * time.Millisecond))
|
|
||||||
n, err := conn.Read(tmp)
|
n, err := conn.Read(tmp)
|
||||||
if err != nil {
|
if n == 0 || err != nil {
|
||||||
// 判断是否是超时错误
|
tmp = nil
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
|
tmpStr := string(tmp[:n])
|
||||||
|
buf.WriteString(tmpStr)
|
||||||
|
|
||||||
|
// 是否有终止符
|
||||||
|
if strings.HasSuffix(tmpStr, ">") || strings.HasSuffix(tmpStr, "> ") || strings.HasSuffix(tmpStr, "# ") {
|
||||||
|
tmp = nil
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
buf.Write(tmp[:n])
|
|
||||||
}
|
}
|
||||||
defer buf.Reset()
|
defer buf.Reset()
|
||||||
|
|
||||||
c.LastResult = buf.String()
|
c.LastResult = buf.String()
|
||||||
|
fmt.Println(c.LastResult)
|
||||||
return c.LastResult, nil
|
return c.LastResult, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,73 +112,11 @@ func (c *ConnTelnet) NewClientSession(cols, rows uint8) (*TelnetClientSession, e
|
|||||||
if c.Client == nil {
|
if c.Client == nil {
|
||||||
return nil, fmt.Errorf("telnet client not connected")
|
return nil, fmt.Errorf("telnet client not connected")
|
||||||
}
|
}
|
||||||
|
conn := *c.Client
|
||||||
|
// 调整窗口
|
||||||
|
conn.Write([]byte{255, 251, 31})
|
||||||
|
conn.Write([]byte{255, 250, 31, 0, cols, 0, rows, 255, 240})
|
||||||
return &TelnetClientSession{
|
return &TelnetClientSession{
|
||||||
Client: *c.Client,
|
Client: conn,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TelnetClientSession Telnet客户端会话对象
|
|
||||||
type TelnetClientSession struct {
|
|
||||||
Client net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭会话
|
|
||||||
func (s *TelnetClientSession) Close() {
|
|
||||||
if s.Client != nil {
|
|
||||||
s.Client.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write 写入命令 不带回车(\n)也会执行根据客户端情况
|
|
||||||
func (s *TelnetClientSession) Write(cmd string) (int, error) {
|
|
||||||
if s.Client == nil {
|
|
||||||
return 0, fmt.Errorf("client is nil to content write failed")
|
|
||||||
}
|
|
||||||
return s.Client.Write([]byte(cmd))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read 读取结果 等待一会才有结果
|
|
||||||
func (s *TelnetClientSession) Read() []byte {
|
|
||||||
if s.Client == nil {
|
|
||||||
return []byte{}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
// 设置读取超时时间为100毫秒
|
|
||||||
s.Client.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
||||||
_, err := s.Client.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}
|
|
||||||
}
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
// CombinedOutput 发送命令带结果返回
|
|
||||||
func (s *TelnetClientSession) CombinedOutput(cmd string) (string, error) {
|
|
||||||
n, err := s.Write(cmd)
|
|
||||||
if n == 0 || err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
tmp := make([]byte, 1024)
|
|
||||||
for {
|
|
||||||
// 设置读取超时时间为1000毫秒
|
|
||||||
s.Client.SetReadDeadline(time.Now().Add(1000 * time.Millisecond))
|
|
||||||
n, err := s.Client.Read(tmp)
|
|
||||||
if err != nil {
|
|
||||||
// 判断是否是超时错误
|
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
buf.Write(tmp[:n])
|
|
||||||
}
|
|
||||||
defer buf.Reset()
|
|
||||||
|
|
||||||
return buf.String(), nil
|
|
||||||
}
|
|
||||||
|
|||||||
74
src/framework/utils/telnet/telnet_session.go
Normal file
74
src/framework/utils/telnet/telnet_session.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package telnet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TelnetClientSession Telnet客户端会话对象
|
||||||
|
type TelnetClientSession struct {
|
||||||
|
Client net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭会话
|
||||||
|
func (s *TelnetClientSession) Close() {
|
||||||
|
if s.Client != nil {
|
||||||
|
s.Client.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write 写入命令 不带回车(\n)也会执行根据客户端情况
|
||||||
|
func (s *TelnetClientSession) Write(cmd string) (int, error) {
|
||||||
|
if s.Client == nil {
|
||||||
|
return 0, fmt.Errorf("client is nil to content write failed")
|
||||||
|
}
|
||||||
|
return s.Client.Write([]byte(cmd))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read 读取结果 等待一会才有结果
|
||||||
|
func (s *TelnetClientSession) Read() []byte {
|
||||||
|
if s.Client == nil {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
// 设置读取超时时间为100毫秒
|
||||||
|
s.Client.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
|
_, err := s.Client.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
// CombinedOutput 发送命令带结果返回
|
||||||
|
func (s *TelnetClientSession) CombinedOutput(cmd string) (string, error) {
|
||||||
|
n, err := s.Write(cmd)
|
||||||
|
if n == 0 || err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
tmp := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
// 设置读取超时时间为1000毫秒
|
||||||
|
s.Client.SetReadDeadline(time.Now().Add(1000 * time.Millisecond))
|
||||||
|
n, err := s.Client.Read(tmp)
|
||||||
|
if err != nil {
|
||||||
|
// 判断是否是超时错误
|
||||||
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buf.Write(tmp[:n])
|
||||||
|
}
|
||||||
|
defer buf.Reset()
|
||||||
|
|
||||||
|
return buf.String(), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user