go-telnet icon indicating copy to clipboard operation
go-telnet copied to clipboard

sample script

Open pyxiscloud opened this issue 7 years ago • 2 comments

Hello Please provide example script for telnet client, which send some strings, then get answer and check it for some conditions? Thank you very much!

pyxiscloud avatar Jun 12 '17 08:06 pyxiscloud

@reejinbouk I found different package, in it provided example script of telnet client. Maybe it will help you: ziutek/telnet

n30fly avatar Jun 16 '17 10:06 n30fly

Hello! For example:

package main

import (
    "github.com/reiver/go-telnet"
    "fmt"
)

// Thin - Connect to thin client
func Thin(host string, port string) {
    if port == "" {
        port = "23" // Default port
    }
    conn, err := telnet.DialTo(host + ":" + port)
    if err != nil {
        fmt.Println(err)
        return
    }

    getCommandResult(conn, "echo hello")
}

func getCommandResult(conn *telnet.Conn, command string) {
    var shebang string = "#"
    var shebangCount int = 0

    conn.Write([]byte(command + "\n"))

    a := make([]byte, 0)
    b := make([]byte, 1)

    for shebangCount < 2 {
        _, err := conn.Read(b)
        if err != nil {
            fmt.Println(err)
        }
        if string(b) == shebang {
            shebangCount++
        }
        a = append(a, b...)
        b = make([]byte, 1)
    }
    fmt.Println(string(a))
}


It will be return:

echo hello
# echo hello
hello
#

aagz avatar Jun 17 '20 00:06 aagz