I need to send commands by telnet to a matrix switcher from Atlona (though, I suppose, it's not a problem with a device, but anyway).
One of commands is x4AVx5.
When I send it from terminal, it works. I can either start connection by telnet <addr> and type x4AVx5 there, or execute { echo "x4AVx5"; sleep 1; } | telnet <addr> terminal command, it works in any way.
But when I try to use reiver/go-telnet package, I can't send this string. This is my code
package main
import (
"fmt"
"github.com/reiver/go-telnet"
)
func main() {
addr := "atlona.mynet.lan:23"
conn, err := telnet.DialTo(addr)
if err != nil {
println("connecting error: " + err.Error())
return
}
n, err := conn.Write([]byte("x4AVx5\n"))
if err != nil {
println("writing error: " + err.Error())
return
}
fmt.Printf("%v bytes written\n", n)
conn.Close()
}
When I run it, any actions don't fail with error, it tells me that 7 bytes were written, but there is no any reaction from my atlona device, it does not receive the command. It should switch 5th output source to 4th input and send an answer the same as the command was (x4AVx5), but none of this happens (checked output of this conn by conn.Read(b) - it's empty, and mainly - the source is not switched).
Tried adding and removing \n before and after the command string, didn't help. Also tried adding time.Sleep(time.Second) between DialTo and Write, as well as between Write and Close - also still doesn't work.
What's interesting, when I use telnet.DialToAndCall(<addr>, telnet.StandardCaller), and then type this command to stdin it also works. But I need to take command from string, not from stdin. Maybe I can somehow pass my string to stdin or build another telnet caller using my string, but I don't know how yet. This also could be a solution, if telnet.Dial + conn.Write won't work.
In fact I can use just os/exec and pass my terminal command to exec.Command, but I thought, that using telnet library is better when operating with telnet, isn't it? And exec terminal command will cause me to install telnet into my docker container with my program. Or this library also needs it?
Well, I would prefer a solution using go-telnet library, than through terminal command, so would be glad to find explanation why this code doesn't work and a fix for it.
\n\rinstead of just\n\r\n, sinceCR LFis defined in that order. If you're just sending some bytes with no authentication or response, did you try just using a plan TCP connection?\ris enough, so\r\nand\n\ralso. Thanks a lot! Spent much time here on this, and it is so easy… Well, I saw that is works with just\nfrom terminal command, so thought that it should work here also.\nform the terminal, because a telnet client sends\r\nas the line ending.