使用golang访问linux telnet服务器
参考https://www.cnblogs.com/dpf-10/p/10429358.html
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) }