惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - LiuYanYGZ

2025.04.15交通事故记录(沈阳、爸爸) 10KG、2KG盘称 摩托车贝纳利302S换机油 excel身份证号验证 在本机A中执行expect脚本:ssh到B,在B机器上从A机器scp文件到B 妈妈的药:血府逐瘀丸、天麻钩藤颗粒、半夏白术天麻汤、杞菊地黄丸、当归补血汤、糠酸莫米松凝胶、尿素维E乳膏 桂附地黄丸、舒筋活血片 辽宁省沈阳市居住证办理2026.01.21 北京健康证(立水桥地铁站附近)第二次去2025.12.28 鼻窦炎、鼻炎、花粉过敏 70迈M800后摄像头改装 D07线 京B摩托车第6年年检,《京顺机动车检测场》150元不用预约 source insight4菜单工具按钮变乱恢复 探索Go语言性能优化:全面解析pprof工具 检查一个IP地址是否在一个网段中 __attribute__((visibility(“default“)))含义 ls | tee 1.txt 如何拿到ls的返回值$? 手机CPU性能天梯图 Poedit Pro国际化翻译编辑器 Proe和Creo的区别 Ubuntu修改DNS方法(临时和永久修改DNS) Ubuntu中apt设置国内源(以阿里云为例) Ghidra(反汇编工具)
使用golang访问linux telnet服务器
LiuYanYGZ · 2025-07-04 · via 博客园 - LiuYanYGZ
package main

import (
    "errors"
    "log"
    "net"
    "strings"
    "time"
)

type TelnetClient struct {
    IP               string
    Port             string
    IsAuthentication bool
    UserName         string
    Password         string
}

func cmd_do(conn net.Conn, cmd string) string {

    var buf [40960]byte
    line_prompt := "root> "
    cmd_w := cmd + "\r\n"
    n_w, err_w := conn.Write([]byte(cmd_w))
    if nil != err_w {
        log.Println("pkg: model, func: Telnet, method: conn.Write, errInfo:", err_w)
        return ""
    }

    conn.SetReadDeadline(time.Now().Add(time.Second * 5))

    max_try := 100
    index := 0
    n_r := 0
    n_r_total := 0
    recv_str := ""
    var err_r error
    for ; index < max_try; index++ {
        time.Sleep(time.Millisecond * 10)
        n_r, err_r = conn.Read(buf[n_r_total:])
        if nil != err_r {
            log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err_r)
            return ""
        }
        n_r_total += n_r
        log.Printf("%d:%d", n_w, n_r_total)
        if (n_r_total <= n_w) {
            continue
        }

        recv_str = string(buf[n_w:n_r_total])
        if (0 > strings.Index(recv_str, line_prompt)) {
            continue
        }
        break
    }

    if max_try == index {
        return ""
    }

    index_begin := strings.Index(recv_str, cmd_w)
    index_end := strings.Index(recv_str, line_prompt)

    if (0 < index_begin) {
        index_begin += len(cmd_w)
    } else {
        index_begin = 0
    }

    if (0 > index_end) {
        index_end = len(recv_str)
    }

    return recv_str[index_begin:index_end]
}

func telnet_Creat(host string, port string,usr string,pass string)  (conn net.Conn, err error) {
    telnetClientObj := new(TelnetClient)
    telnetClientObj.IP = host
    telnetClientObj.Port = port
    telnetClientObj.IsAuthentication = true
    telnetClientObj.UserName = usr
    telnetClientObj.Password = pass
    conn,err =telnetClientObj.Telnet(20)

    return conn,err
}
func (this *TelnetClient) Telnet(timeout int) (conn net.Conn, err error) {  
    raddr := this.IP + ":" + this.Port   
    
    conn, err = net.DialTimeout("tcp", raddr, time.Duration(timeout)*time.Second)  
    if nil != err {     
        log.Print("pkg: model, func: Telnet, method: net.DialTimeout, errInfo:", err)     
        return 
    }  
    
    conn.SetReadDeadline(time.Now().Add(time.Second * 5))

    if false == this.telnetProtocolHandshake(conn) {      
        log.Print("pkg: model, func: Telnet, method: this.telnetProtocolHandshake, errInfo: telnet protocol handshake failed!!!")      
        return conn, errors.New("Access denied")
    }   

    return conn, err
}

func (this *TelnetClient) telnetProtocolHandshake(conn net.Conn) bool {
    var buf [4096]byte
    log.Print("telnetProtocolHandshake")
    n, err := conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    // log.Print("1====",string(buf[0:n]))
    // log.Printf("%x",(buf[0:n]))
    buf[0] = 0xff
    buf[1] = 0xfc
    buf[2] = 0x25
    buf[3] = 0xff
    buf[4] = 0xfe
    buf[5] = 0x01
    n, err = conn.Write(buf[0:6])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    // log.Print("2====", string(buf[0:n]))
    // buf[0] = 0xff
    // buf[1] = 0xfe
    // buf[2] = 0x03
    // buf[3] = 0xff
    // buf[4] = 0xfc
    // buf[5] = 0x27
    // n, err = conn.Write(buf[0:6])
    // if nil != err {
    //     log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
    //     return false
    // }
    // log.Print("2.1====",string(buf[0:6]))

    // n, err = conn.Read(buf[0:])
    // if nil != err {
    //     log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
    //     return false
    // }
    // log.Print("3====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.UserName + "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 50)

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    // log.Print("4====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.Password+ "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 50)
    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }

    // log.Printf("5====%s\n",string(buf[0:n]))
    // log.Printf("5====%s|5isover, n=%d\n",string(buf[0:n]), n)

    if strings.Contains(string(buf[0:n]), "Access denied") {
        return false
    }

    // buf[0] = 0xff
    // buf[1] = 0xfc
    // buf[2] = 0x18
    // n, err = conn.Write(buf[0:3])
    // if nil != err {
    //     log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
    //     return false
    // }
    // log.Printf("5.1====%s|5isover",string(buf[0:n]))

    // n, err = conn.Read(buf[0:])
    // if nil != err {
    //     log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
    //     return false
    // }
    // log.Print("6====",string(buf[0:n]))

    return true
}

func Dev_cmd_do(dev_IP string, cmd_arr []string) map[string]string {
    host := dev_IP
    port := "8000"
    usr := "root"
    pass := "root@1234"

    m_out := make(map[string]string, 0)

    conn, err:= telnet_Creat(host, port, usr, pass)
    if nil != err {
        log.Print(err)
        return m_out
    }
    defer conn.Close()  
//后续对conn进行操作即可

    for _, cmd_each := range cmd_arr {
        cmd_out := cmd_do(conn, cmd_each)
        log.Printf("req=%s, resp=%s", cmd_each, string(cmd_out))
        m_out[cmd_each] = cmd_out
    }
    // log.Printf("%s====>>>>%s<<<<,L=%d", cmd, string(cmd_out), len(cmd_out))

    return m_out
}


func main() {
    // host := "172.20.10.198"
    host := "172.20.40.64"

    // cmd := []string{"rsp_get_version", "rsp_get_link_status -p0"}
    cmd_arr := []string{"rsp_get_version", 
                        "rsp_get_band_freq"      ,
                        "rsp_get_frm_struct"     ,
                        "rsp_get_link_status -p0",
                        "rsp_get_link_status -p1",
                        "rsp_get_link_status -p2",
                        "rsp_get_link_status -p3",
                        "rsp_get_link_status -p4",
                        "rsp_get_link_status -p5",
                        "rsp_get_link_status -p6",
                        "rsp_get_link_status -p7",
                        "rsp_get_link_status -p8",
                        "rsp_get_link_status -p9",
                        "rsp_get_version"        }

    Dev_cmd_do(host, cmd_arr)
}