Netcat Triggers Connection Reset Error When Program Exits - bash

I'm using netcat -e $prog, which is fine, but whenever $prog exits, netcat hangs up on the original connection. Any thoughts on how to make it disconnect with a clean FIN-ACK? Or why it's not in the first place?
$ nc localhost 2000
*random data that should just cause it to exit cleanly*
read(net): Connection reset by peer
$

Related

How can I exit the FTP session if the session hangs?

I tried connecting to an ftp server with the command:
ftp [IP]
but only got connection timed out error and then the FTP session hangs:
FTP hangs
and I can't exit the session..I can't type in anything. How do I come out of this, i.e. how do I exit the session when it is at this state?
I am running the commands in git bash.
I also tried ctrl+C but that didn't work either, nothing happened.

Terminate Curl Telnet session in bashscript

Working on automating telnet connectivity from various hosts running the script from specified host with curl telnet call.
However as are aware for telnet once we get connected status for any hosts we have to pass an escape character to terminate the telnet sessions, but in bash script I need to terminate the session as soon as we get Connected/Refused response from the target endpoint or after some seconds of running the telnet session .
PFB Script where telnet connectivity is checked through Curl call, so I need is there anyway in curl that we can terminate the telnet session in curl as soon as we get the response or terminate the session in some milliseconds/seconds.
Code:
#!/bin/bash
HOSTS='LPDOSPUT00100 LPDOSPUT00101'
for S in ${HOSTS}
do
echo "Checking Connectivity From Host : ${S}"
echo ""
ssh -q apigee#${S} "curl -v telnet://${TargetEndPoint}:${Port}"
done
You could run it in the timeout command to make it terminate after a certain amount of time.
ssh -q apigee#"$S" "timeout 5s curl -v telnet://${TargetEndPoint}:${Port}"
would terminate it after 5 seconds if it hadn't already exited on its own.
Perhaps curl isn't the right tool for this job though. Have you considered using nc instead?
ssh -q apigee#"$S" "nc -z ${TargetEndPoint} $Port"
will likely do what you want.

Telnet : connect, send command and exit in just one line

I try to use telnet in a script (to be use in python program). I want to do the connection, send a command, and exit the connection in juste one line. The command i want to send is to start a program on a remote machine but I d'ont want to wait the end of this program to exit the telnet connection.
I try to do : "echo myCommand | netcat 192.168.1.50 23" but it waits the end of the program.
thanks for your help
Use bash builtin tcp socket feature:
echo yourCommand >/dev/tcp/192.168.1.50/23

SSH command within a script terminates prematurely

From myhost.mydomain.com, I start a nc listener. Then login to another host to start a netcat push to my host:
nc -l 9999 > data.gz &
ssh repo.mydomain.com "cat /path/to/file.gz | nc myhost.mydomain.com 9999"
These two commands are part of a script. Only 32K bytes are sent to the host and the ssh command terminates, the nc listener gets an EOF and it terminates as well.
When I run the ssh command on the command line (i.e. not as part of the script) on myhost.mydomain.com the complete file is downloaded. What's going on?
I think there is something else that happens in your script which causes this effect. For example, if you run the second command in the background as well and terminate the script, your OS might kill the background commands during script cleanup.
Also look for set -o pipebreak which terminates all the commands in a pipeline when one of them returns with != 0.
On a second note, the approach looks overly complex to me. Try to reduce it to
ssh repo.mydomain.com "cat /path/to/file.gz" > data.gz
(ssh connects stdout of the remote with the local). It's more clear when you write it like this:
ssh > data.gz repo.mydomain.com "cat /path/to/file.gz"
That way, you can get rid of nc. As far as I know, nc is synchronous, so the second invocation (which sends the data) should only return after all the data has been sent and flushed.

How to connect stdin of a list of commands (with pipes) to one of those commands

I need to give the user ability to send/receive messages over the network (using netcat) while the connection is stablished (the user, in this case, is using nc as client). The problem is that I need to send a line before user starts interacting. My first attempt was:
echo 'my first line' | nc server port
The problem with this approach is that nc closes the connection when echo finishes its execution, so the user can't send commands via stdin because the shell is given back to him (and also the answer from server is not received because it delays some seconds to start answering and, as nc closes the connection, the answer is never received by the user).
I also tried grouping commands:
{ echo 'my first line'; cat -; } | nc server port
It works almost the way I need, but if server closes the connection, it will wait until I press <ENTER> to give me the shell again. I need to get the shell back when the server closes the connection (in this case, the client - my nc command - will never closes the connection, except if I press Ctrl+C).
I also tried named pipes, without success.
Do you have any tip on how to do it?
Note: I'm using openbsd-netcat.
You probably want to look into expect(1).
It is cat that wait for the 'enter'.
You may write a script execute after nc to kill the cat and it will return to shell automatically.
You can try this to see if it works for you.
perl -e "\$|=1;print \"my first line\\n\" ; while (<STDIN>) {print;}" | nc server port
This one should produce the behaviour you want:
echo "Here is your MOTD." | nc server port ; nc server port
I would suggest you use cat << EOF, but I think it will not work as you expect.
I don't know how you can send EOF when the connection is closed.

Resources