I have a perl script that, when it is not numerous, looks like this:
my $randport = int(10000 + rand(1000));
The fact is that the target $ ip is located on a link with very unpredictable bandwidth, so the tunnel may appear immediately, it may take some time, it may not occur at all. Thus, sleep is needed to give the tunnel some time to get up and work.
So the question is: how to check if the tunnel is working? 10 seconds is a really unwanted delay if the tunnel appears immediately. Ideally, I would like to check if this happens and continue creating the telnet object, if so, for a maximum of, say, 30 seconds.
Edit: Ping does not help me mouch, since the remote end of the tunnel is usually up, but with a very large number of packets l
Solved: Extrapolating from the prompt suggested by mikebabcock, sleep 10 been replaced by this block, which works like a charm:
my $starttime = time(); while (1) { # Check for success if (system("nc -dzw10 localhost $randport > /dev/null") == 0) { last } # Check for timeout if (time() > $starttime + 30) { &fail() } # 250ms delay before recheck select (undef, undef, undef, 0.25); }
source share