Capture Telnet output to a file by running in the background - linux

I am having a linux powered board on which I wanted to capture the telnet output to a file. I tried doing it as shown below-
telnet localhost xxxx >> /mnt/sd-xxx/log/file.txt &
as well as
telnet localhost xxxx | tee /mnt/sd-xxx/log/file.txt &
and
telnet localhost xxxx -f /mnt/sd-xxx/log/file.txt &
But it's unable to go in background in all of the above cases. I also tried to keep it in a script,but this also doesn't work and the program crashes. How can I capture the telnet output and redirect to a file by running it as a background process.
Thanks in Advance.

Have you tried curl?
curl telnet://localhost:xxxx >> /mnt/sd-xxx/log/file.txt &

Related

Why is the netcat response not displaying in browser in Mac OS?

I've tried using netcat in various ways.
The common just exits
echo "Response from server" | nc -l 127.0.0.1 8080
I get the following error in browser net::ERR_CONNECTION_REFUSED
nc -l localhost 8080 < temp.resp
I just simply want to print some text from nectar to browser and maybe later pass some headers along with it too. But right now I am not able to figure out what is going wrong.
I've looked in the man page and tried -w and -I options but none of them are working.

open telnet using shell and passing commands

I am new to linux and shell scripting. I want to connect to localhost and interact it.
#! /bin/bash
(exec /opt/scripts/run_server.sh)
when i execute this bash script, it starts listening on a port.
Listening on port xxxxx
Now i want to issue this command "telnet localhost xxxxx"
I tried something like this:
#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
It is still listening on the port. But i think second command is not running. I expect another window showing that it is being connected like this.
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>
The reason why i executing these as a script is that, automatically in the server i need to carry out some process by issuing certain commands like this "scm" "parse" etc.....
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>scm
Entering scheme shell; use ^D or a single . on a line by itself to exit.
guile> (parse "i eat apple")
I have lots of text coming. Manually i cant issue this parse command for each and every sentence. so i want to automate. So i need to write a script for connecting to the server and interacting.
Any guidelines. Finally How to interact/send commands to this guile shell?
One way to login to the linux server as a same or different user and run some command or .sh script (very useful for post-commit hooks or cron jobs) is to use program called sshpass, for example a cron job command or svn post-commit hook would look like this:
/usr/bin/sshpass -p 'password' /usr/bin/ssh
-o StrictHostKeyChecking=no -q user#localhost 'any command'
Just replace password with your password, and user with your user, and put command that you need to run as that particular user...
To install sshpass it on ubuntu just type
apt-get install sshpass
Or on CentOs
yum install sshpass
I solved this with the netcat (nc) command.
$ echo "command1\ncommand2\n" | nc localhost xxxxx
I could manually connect to localhost using telnet localhost xxxx and then i can pass commands from shell to localhost like this.
If you need to use telnet, this solution may help you. Otherwise, use ssh, as other answer suggests.
You can use anything that produces output to write lines one by one, followed by "\r\n", and pipe these lines to ncat, e.g.:
echo -e "command1\r\ncommand2\r\n" | ncat localhost 5000
-e option makes echo interpret "\r\n" as special symbols.

How to redirect stdout & stdin to telnet connection?

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

BASH - how to make this always running from system boot and on crash restart?

I have this protocol port open to read remotely from Python, PHP applications but daily it crash and the port is unavailable as a result Python, PHP all client application fails
$ cat /var/tmp/server.sh
#!/bin/bash
while true; do tail -f /usr/local/freeswitch/log/freeswitch.log | nc -l -p 9999 -q 1 &
Q. Is there anyway to make this script always running like service this start or stop and if its crashed that somehow it automatically again get restarted ? Any advise or link to do such thing? i am using CentOS 6.x
Put your script in /etc/inittab as following
id:1:respawn:/var/tmp/server.sh
Refer to http://linux.about.com/od/commands/l/blcmdl5_inittab.htm for more information about the /etc/initab file.
After editing /etc/inittab restart your system.

Writing output from a socket

I have 2 machines A and B.
In machine A, I do
echo "Hello World" > /dev/tcp/{Bs_ip}/12345
In machine B, how do I write a script that runs in the background, listens on port 12345, and prints whatever it receives from port 12345 to stdout?
BTW both machines are running Red Hat Enterprise Linux AS 4.
Thanks
You can do that using netcat:
nc -l -p 123456
If you want to be able to handle multiple connections you will have to use a loop.
You can use netcact (nc) or netcat on steroids, ie socat. I gave a link to the examples section of the man page, so that you can see how powerful socat is.
socat TCP4-LISTEN:12345 -
Should do what you want

Resources