reiver / go-telnet

Package telnet provides TELNET and TELNETS client and server implementations, for the Go programming language, in a style similar to the "net/http" library that is part of the Go standard library, including support for "middleware"; TELNETS is secure TELNET, with the TELNET protocol over a secured TLS (or SSL) connection.
https://godoc.org/github.com/reiver/go-telnet
MIT License
266 stars 83 forks source link

sample script #4

Open pyxiscloud opened 7 years ago

pyxiscloud commented 7 years ago

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!

n30fly commented 7 years ago

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

aagz commented 4 years ago

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
#