1

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.

5
  • 1
    I didn't use this library before, but the first thing I would try is sending \n\r instead of just \n Commented Dec 9, 2025 at 16:58
  • 1
    I think Burak meant \r\n, since CR LF is 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? Commented Dec 9, 2025 at 17:12
  • Wow, really… That's what I haven't tried yet. Now tried different variants, and revealed that just \r is enough, so \r\n and \n\r also. Thanks a lot! Spent much time here on this, and it is so easy… Well, I saw that is works with just \n from terminal command, so thought that it should work here also. Commented Dec 9, 2025 at 17:13
  • 1
    You're not using only \n form the terminal, because a telnet client sends \r\n as the line ending. Commented Dec 9, 2025 at 17:28
  • Ok, see now. Thanks for explanation Commented Dec 9, 2025 at 17:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.