How to redirect stdout & stdin to telnet connection? - linux

I am running embedded linux program, so that the kernel init script automatically start the program, and the stdin/stdout are going through the serial device, which is also the shell.
When I connect to target with telnet, I don't see the same stdin/stdout of the program.
Maybe I can redirect console stdin/stdout to telnet connection ?
What ways do I have to gain such capabilities using the telnet connection ?
Thanks,
Ran

I don't know if it is possible with telnet, but you can mock it through netcat. Just like below:
<STDOUT> | netcat -t www.example.com 80

Related

How to hide telnet connection logs from getting printed in screen

I have a script which telnet to remote system & user can interact with remote system. But i want to hide telnet connection logs from getting printed for security reasons. I tried all the redirection techniques like (> , 1>, 2>), but my purpose is not served. "1>" is not allowing to interact with remote system.
How to redirect/hide only telnet connection logs (or first 3 connection lines) below & make telnet session interactive ?
script :
#!/bin/bash
telnet 1.2.3.4 7777
sample issue execution :
~/redirect.sh
Trying 1.2.3.4... // redirect
Connected to 1.2.3.4.
Escape character is '^]'.
login:
sample expected execution :
~/redirect.sh
login:
There is no easy fix for this, as those three lines are simply printf() in the code. It would be a great deal of effort to remove those lines and allow interactive connections.
However, it is a simple client side change to modify the telnet client source and recompiling:
Download inetutils-2.3 from here.
Extract with tar -xJvf inetutils-2.3.tar.xz.
cd inetutils-2.3.
./configure
Use the patch in this answer: patch telnet/commands.c < /path/to/telnet.patch
patching file telnet/commands.c
make
Then test:
2>/dev/null ./telnet/telnet 192.168.100.1 22
SSH-2.0-OpenSSH_8.6
Copy this version of telnet into your PATH somewhere. Possibly rename it stelnet.

Redirect TCP data to ttyS0

I have a closed application running on a different, but network accessible, Linux OS that is using SerialIO to open /dev/ttyS0.
How can I write to ttyS0 from a different device so that the existing application will see what I'm writing as actual serial data?
Ive tried quite a few different socat commands and havent had luck.
socat -d -d /dev/ttyS0,raw,echo=0,b9600 tcp-l:6174,reuseaddr
socat -d -d pty,link=/dev/ttyS0,raw,echo=0 tcp-l:6174,reuseaddr
Any ideas on what is the best way to do this so that the existing (untouchable) application will think nothing has happened?
Take a look at the ser2net daemon. It is able to act as a TCP serial server for either raw or RFC2217 connections.

terminal in raw mode - stty(1)

from an ESXi 5.0 shell I would have access to a VM virtual serial interface (VM is running on the ESXi hypervisor). Using netcat (nc) available on ESXi, is possible to connect to the VM virtual serial interface via a UNIX socket binded to it (named pipe).
Now, to emulate a "real" terminal connected to the VM serial interface, I've set the pseudo-terminal in raw mode via "stty raw" command (issued on ESXi shell). Access to the OS running in the VM is fine but I can see that sent characters are echoed back
I've tried passing -echo to stty without lucky: the terminal seem blocked and the only way to recover is disconnect and riconnect again...
Someone can help me ?
I've solved concatenating stty and nc as follows:
stty raw -echo; nc -U 'unix socket'
which is the difference between stty and nc "concatenation" via ";" and just run the two commands one after the other ("stty raw -echo" and then "nc -U unix socket") ?

Continuously send the content of a file through a server socket with netcat

I have a linux machine which is listening for connections on port 4450. Where there is an incomming connection, this is supposed to send continuously over the socket the content of a file. Did you do this before ?
What I've done so far was to send once the content of the file like this:
x=$(filename); echo $x | nc -l 4450
On the client side I have an Android app, which connects to the server and then using a BufferedReader gets the data from the stream and processes it.
Any help would be highly appreciated.
Thanks
Use socat instead of netcat (nc). With socat you can do almost everything that can be done with netcat. But socat has a lot more features and is easier to use.
socat TCP-LISTEN:4450,fork OPEN:/tmp/filename,rdonly
You can also use the output of a command instead of some file contents:
socat TCP-LISTEN:4450,fork EXEC:/bin/date

Linux; How do I find logs if a program I'm running uses certain ports?

I am running CentOS 5 with csf firewall. I'm running a program that can't connect to another server (using some port that is blocked by csf I presume). Where is the log file for 'ports'?
Netstat is the command to use to get ports and network activity. To diagonise server processes I usually use:
netstat -tln
This yields port numbers in tcp mode listening. To identify associated processes you can also use -p to grab the pid. Here is the IANA ports list.
I found my answer right after searching a few more threads.
# tail -f /var/log/messages
Shows the UDP message but not the port.... Hmm....

Resources