Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telnet client in go

Tags:

go

telnet

I am trying to send 'hello world' to the telnet server from go client. In the documentation I have found example:

var caller telnet.Caller = telnet.StandardCaller    
telnet.DialToAndCall("localhost:5555", caller)

What is the next step to send 'helloworld' now?

like image 493
Rudziankoŭ Avatar asked Dec 12 '25 08:12

Rudziankoŭ


2 Answers

Example of programmatic connection using go-telnet

func SetTest() {
    conn, _ := telnet.DialTo("localhost:5555")
    conn.Write([]byte("hello world"))
    conn.Write([]byte("\n"))

}
like image 163
Rudziankoŭ Avatar answered Dec 14 '25 00:12

Rudziankoŭ


In the example below you can see that the CallTELNET uses stdin and stdout to allow the user of the program to communicate through telnet. You can send "hello world" by running the program and typing the desired text you wish to send followed by the enter key.

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"

    "github.com/reiver/go-oi"
    "github.com/reiver/go-telnet"
)

type caller struct{}

func (c caller) CallTELNET(ctx telnet.Context, w telnet.Writer, r telnet.Reader) {
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        oi.LongWrite(w, scanner.Bytes())
        oi.LongWrite(w, []byte("\n"))
    }
}

func main() {
    fmt.Printf("Dial to %s:%d\n", "localhost", 8080)
    err := telnet.DialToAndCall(fmt.Sprintf("%s:%d", "localhost", 8080), caller{})

    if err != nil {
        log.Fatal(err)
    }
}

Examples found here and here

like image 38
smokedice Avatar answered Dec 14 '25 00:12

smokedice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!