:: [DNG] Testing network connectivity
Forside
Slet denne besked
Besvar denne besked
Skribent: Steve Litt
Dato:  
Til: dng
Emne: [DNG] Testing network connectivity
Hi all,

In daemontools-inspired inits, you express dependencies via if
statements in the run script. The best way I've found, so far, to do
this in Devuan is to use the following script, called netup.sh:

========================================
#!/bin/sh
echo 'quit' | nc -w2 $1 $2 2>/dev/null 1>/dev/null
return $?
========================================

The nc command was already installed on my from-the-factory Devuan.
This could be used in a run script as follows:


========================================
#!/bin/sh
if netup.sh google.com 80; then
exec sshd -d
fi
sleep 1
========================================

Or, more tersely:

========================================
#!/bin/sh
netup.sh google.com 80 && exec sshd -d
sleep 1
========================================


The reason I used nc instead of ping is that in many virtual machine
host situations, you can't ping something on the Internet but you can
nc it. So this works both on metal and virtual.

The -w2 arg to nc limits response time to 2 seconds, instead of hanging
for an inordinate amount of time. If a server hasn't responded within 2
seconds, there's something wrong anyway.

Obviously, this could be used to test any server for some sort of
response to any port. Of course, if you wanted a specific response,
you'd probably use a test more specific to the service provided by the
host.

SteveT

Steve Litt
July 2017 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz