¿Cómo modificar la flujo de entrada de Netcat?
-
14-11-2019 - |
Pregunta
Estoy creando una interfaz TCP / IP a un dispositivo en serie en una máquina RedHat Linux.NetCat en un guión bash se usó para lograr esto sin problemas.
nc -l $PORT < $TTYDEVICE > $TTYDEVICE
El problema es que el dispositivo serie utiliza devoluciones de carro ('\ R') para que finalice las líneas en sus respuestas.Quiero traducir esto a ("\ r \ n") para que las máquinas de Windows Telneting en puedan ver la respuesta sin ningún problema.Estoy tratando de averiguar cómo hacerlo con una solución de bash simple.También tengo acceso a PTY para configurar el dispositivo serie, pero no hay "\ r" a "\ r \ n" traducir en el lado de entrada (de lo que puedo decir).
Intenté usar TR en el lado de entrada de Netcat, pero no funcionó.
#cat $TTYDEVICE | tr '\r' '\r\n' | nc -l $PORT > $TTYDEVICE
¿Alguna idea?
Solución
This is overly difficult with standard tools, but pretty easy in perl (although perl is pretty much a standard tool these days):
perl -pe 's/\r/\r\n/g'
The version above will likely read the entire input into memory before doing any processing (it will read until it finds '\n', which will be the entire input if the input does not contain '\n'), so you might prefer:
perl -015 -pe '$\="\n"'
Otros consejos
Your problem is that the client that connects to $PORT probably does not have a clue that it is working with a tty on the other side, so you will experience issues with tty-specific "features", such as ^C/^D/etc. and CRLF.
That is why
socat tcp-listen:1234 - | hexdump -C
telnet localhost 1234
[enter text]
will show CRLFs, while
ssh -t localhost "hexdump -C"
[enter text]
yields pure LFs. Subsequently, you would e.g. need
ssh -t whateverhost "screen $TTYDEVICE"
tl;dr: netcat won't do it.
There are a number of versions of netcat (GNU and BSD) but you might want to try:
-C Send CRLF as line-ending