帶互動的telnet小工具,golang版

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package netTools //main //import ("fmt""net""strconv""strings""time")func smain() {lis, err := net.Listen("tcp", ":1789")if err != nil {fmt.Println(err)}defer lis.Close()for {con, _ := lis.Accept()go handler(con)}}func handler(con net.Conn) {defer con.Close()buf := make([]byte, 20)n, _ := con.Read(buf)fmt.Println(string(buf[:n]))con.Write(buf)con.Write(buf)}//func main() {//buf, err := Telnet([]string{"w_Hello World", "r_50", "r_30"}, "127.0.0.1:1789", 20)//fmt.Println(err)//fmt.Println(string(buf))//}func PortIsOpen(ip string, timeout int) bool {con, err := net.DialTimeout("tcp", ip, time.Duration(timeout))if err != nil {return false}con.Close()return true}func Telnet(action []string, ip string, timeout int) (buf []byte, err error) {con, err := net.DialTimeout("tcp", ip, time.Duration(timeout)*time.Second)if err != nil {return}defer con.Close()con.SetReadDeadline(time.Now().Add(time.Second * 5))for _, v := range action {l := strings.SplitN(v, "_", 2)if len(l) < 2 {return}switch l[0] {case "r":var n intn, err = strconv.Atoi(l[1])if err != nil {return}p := make([]byte, n)n, err = con.Read(p)if err != nil {return}buf = append(buf, p[:n]...)fmt.Println(buf)case "w":_, err = con.Write([]byte(l[1]))}}return}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.