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

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.

Related

how to stop ssh script when golang ssh session is disconnect with remote server

When I using golang ssh into one server, found my ssh session between golang program and remote server disconnect with error (wait: remote command exited without exit status or exit signal).
So I done a test, after golang program build a connection with remote ubuntu server, I login that server, and using
kill -9 <ssh-session pid>
to kill that ssh session, and the golang program return the same error
wait: remote command exited without exit status or exit signal
However, I found the ssh script that golang program had sent into remote server are still running. That is not what I want, I just want to stop the ssh script together with ssh session's disconnection.
How can I stop the ssh scripts' running when the ssh session's end?

Bash script to write to unix socket and then read a response but ONLY AFTER the welcome message

I am connecting to a server that initiates an SSH tunnel for me so I can connect to a remote device.
I can do an interactive socat connexion and manually issue commands like this:
Connected to soundwave server v131 (welcome message)
tunnel r 7 localhost:22 (my command)
Attempting to initiate a tunnel session ID [55] on local port 30054. (response)
The text in parenthesis is just my notes. They aren't actually part of the commands or responses.
I know I can send a message to a unix socket using socat
echo "tunnel r 7 localhost:22" | socat UNIX-CONNECT:data/files/monitor.socket STDOUT
And I saw some posts about being able to write a command and read a command with socat.
But the software has limitations that it only listens for commands until after the welcome message is issued.
So, is there a way with socat, nc, or any other tool to connect to a unix socket automatically, read the welcome message, write the tunnel command, and then parse the response to get the port so I can open up an SSH session?
Thanks so much.

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.

Stop SFTP/FTP connection while the active connection is not transferring data in Unix Shell Scripting

I'm having an issue but I can't see something related with this.
I'm having an issue, something is happening while I'm performing an FTP connection with a server is transferring a file, but for some reason sometimes is stuck but I would like to prevent have the connection opened, there is a way to see if the FTP connection is not transferring, close the connection?
I really don't have any code due I'm not sure if this is possible,
Any idea what can I do at this point?
If it is closing the connection while you are transferring files, then it's either your FTP/SFTP client, server, or network. First, Switch to a different FTP/SFTP client. Some have more tools for analysis than others. I have had to do this before. If that doesn't work, check the internet connection or contact your system/network administrator.
there is a way to see if the FTP connection is not transferring, close
the connection?
If you are downloading a file, you can indirectly see the FTP transferring by watching the file's size:
name=$1
size=0
while sleep 10
set -- `ls -s $name`
[ "$1" -gt $size ]
do size=$1
done
exit 1
The above script (let's call it growing) runs while the file (passed as a parameter) grows.
In your script you could write something like
growing file || pkill ftp &
before you start the FTP. If the file stops growing for ten seconds, ftp would be killed and the connection thereby closed. If ftp terminates normally, you could kill $! or just let growing end.

Netcat Triggers Connection Reset Error When Program Exits

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
$

Resources