[R] Test internet presence

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Mon Sep 11 14:48:21 CEST 2006

On 11-Sep-06 Gregor Gorjanc wrote:
> It seems that 'internal' method was used (I use R 2.3.1 under
> Linux) as indicated in help page of download.file. I could
> use wget or lynx methods, but these two must be available,
> so this is not really portable. Are there any other options
> for testing internet access? I am thinking that this might be
> more relevant for R-devel. I will wait a bit
> before moving there.
> -- 
> Lep pozdrav / With regards,
>     Gregor Gorjanc

Hi Gorjanc,
Since you are using Linux, I think you should ask R to delegate
the test to the system.

If you have a script, in executable file ("755") say "test.inet.sh",
which says something like

if ping -c 1 <something> ; then
    export NET_UP="YES"
    export NETP_UP="NO"

where "<something>" is the IP address or name of an external host
which responds to 'ping' (some will not, depending on their firewall
settings), then you can use on R:

if( sys.getenv(NET_UP") == "YES" ) { ... } else { ... }

For example (nothing to do with R, but shows the principle),
I have the following script to set my system time and hardware
clock from whichever one of 3 NTP servers is willing to respond:

if /bin/ping -c 1 ntp0.zen.co.uk ; then
    export NETTIME="/usr/sbin/ntpdate -u ntp0.zen.co.uk"
  elif /bin/ping -c 1 ntp2b.mcc.ac.uk ; then
    export NETTIME="/usr/sbin/ntpdate -u ntp2b.mcc.ac.uk"
  elif /bin/ping -c 1 ntp2c.mcc.ac.uk ; then
    export NETTIME="/usr/sbin/ntpdate -u ntp2c.mcc.ac.uk"
    export NETTIME=""
if [ "$NETTIME" != "" ] ; then
  sleep 1
  sleep 1
  /sbin/clock -u -w

which also illustrates how to allow for the possibility that
the "default" server might not be responding at the time, so
it has 2 fallback servers.

Hoping this helps,

E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 11-Sep-06                                       Time: 13:42:22
------------------------------ XFMail ------------------------------

More information about the R-help mailing list