Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sample script #4

Open
pyxiscloud opened this issue Jun 12, 2017 · 2 comments
Open

sample script #4

pyxiscloud opened this issue Jun 12, 2017 · 2 comments

Comments

@pyxiscloud
Copy link

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
Copy link

n30fly commented Jun 16, 2017

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

@aagz
Copy link

aagz commented Jun 17, 2020

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
#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants