0

I don't understand how to properly setup a data transmission between two host with unstable ethernet connection.

This is my /simple/bash/script.sh

#!/bin/bash


while [ true ]; do
    cat /dev/virtual  | nc -v 192.168.1.1 5005 || echo "nc failed" && exit   
    sleep 5s
done

exit

If I manually start it and on the destination netcat isn't running it would say:

5005 (tcp) failed: Connection refused

But it would not exit.

I need it the other way:

  1. check netcat connection is ok.
  2. then pipe cat /dev/virtual.

Is there a way to catch the netcat status and then if it is failed: connection refused restart the main bash script?

Hauke Laging
  • 90,279

1 Answers1

0

This should work:

#!/bin/bash

if ! nc -z 192.168.1.1 5005 ; then
    sleep 1 && "$0" & exit;
fi

nc -v 192.168.56.203 5005 < /dev/virtual

Explanation:

  • nc -z will check if someone is listening on the other side
  • if no one is listening, the program will sleep for 1 sec (obviously you can change this) and then it will start another instance of itself and exit
  • if someone is listening, then the program will send /dev/virtual
Francesco
  • 836