go-telnet
go-telnet copied to clipboard
sample script
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!
@reejinbouk I found different package, in it provided example script of telnet client. Maybe it will help you: ziutek/telnet
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
#