Search code examples
pythonlinuxbashnetcat

Netcat auto reconnect on disconnection


I've set up a series of netcat connections in a Bash/Debian Linux environment to remote servers which periodically send me messages. I'm continuously writing the results of these messages to a local file as so:

nc 192.168.1.38 23 >> results
nc 192.168.1.39 4501 >> results
nc 192.168.1.40 8080 >> results

Everything works great. But periodically, one of the remote servers kills the netcat connection (i.e. performance reasons, overloading, etc.). When this happens I have to manually reconnect to the server. Is there an automated method of immediately reconnecting, so that no intermediary messages are lost?

Suggestions in bash or python preferred.


Solution

  • You can use an infnite loop. When netcat ends it'll just start again... Infinitely...

    Try this (bash script)

    while true
    do
        nc 192.168.1.38 23 >> results
        nc 192.168.1.39 4501 >> results
        nc 192.168.1.40 8080 >> results
    done