Check if the connector is closed in bash?

I have a file descriptor pointing to a socket (sample code below).

exec 3<>/dev/tcp/localhost/9999 echo -e "Some Command\n" >&3 

Sometimes this socket is closed and it needs to be reopened (server reboot).

How can I check if a socket is available (fd # 3 in this case)?

An echo will always be successful, regardless of whether the socket has already been closed or not.

+6
source share
5 answers

The solution is server feedback.

When you send a request to the server, it needs to respond to it.

 exec 3<>/dev/tcp/localhost/9999 echo -e "Some Command\n" >&3 sleep 5 # example max time given to server to respond cat <&3 #receive an answer check is correct, restart server otherwise 

EDIT: using netcat to determine if a port is open

 netcat -w 3 -z www.google.com 80 if[ $? -eq 0 ] then echo port open else echo port closed fi 
+4
source

Some time has passed since op posted this so that they do not see it, but could help someone else.

Anyway, I studied this problem and I found the following.

The open fd (file descriptors) of the process are listed in the / proc // fd section.

 exec 3<>/dev/tcp/localhost/9999 #check if still connected if [ $(ls /proc/$$/fd | grep -w "3") == 3 ]; then #send data echo -e "Some Command\n" >&3 else #perform reconnect exec 3<>/dev/tcp/localhost/9999 fi 

This has not been verified, but should be basically in order. There may be some improvements. There is also a window in which fd goes between your check and the letter in fd. However, this applies to all decisions.

+3
source

I will add my own final solution (in compressed psudo code):

 { while true; read file; write to STDOUT } | { while true; netcat command; write STDIN to buffer when/if netcat exits; loop to restart netcat & first process buffered data if exists; } 

Separates data output (reading a file) and data processing (sending them to a socket or buffering to a file if there is no socket). It uses the channel to provide a temporary buffer when network problems occur.

The STDIN of the second code block buffers output from the first code block. If netcat cannot process data on stdin, it will want to write it to a spooled file and try to restart netcat. Thus, you do not have any time interval between checking that the socket is open (something else complicated) and the actual record (which may still not work after checking its opening).

+1
source

You can use the netstat , grep for the port / address you want, and then cut just a status field. Please note that the socket may appear as β€œESTABLISHED” for some time after losing the connection, especially if you are not sending any data to it.

0
source

Try writing to fd:

  if!  echo some text> & 3;
   then echo The port is closed> & 2
 fi

Please note that I do not suggest writing extra data for verification if the file descriptor remains valid, but I suggest you just try to write down any data you want, and then check that it worked. If the recording failed, open the socket again and record again.

0
source

Source: https://habr.com/ru/post/892113/


All Articles