[R] Test internet presence
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Sep 12 07:53:00 CEST 2006
Here is a variation for Windows. The second line returns TRUE or FALSE
and may need to be varied if the output of ping is not the same on your
system as on mine:
ping <- system("ping www.google.com", intern = TRUE)
as.numeric(strsplit(grep("Received", ping, value = TRUE), "[ ,]")[[1]][8]) > 0
On 9/11/06, Gregor Gorjanc <gregor.gorjanc at bfro.uni-lj.si> wrote:
> Thank you. Your solution is usable but unfortunatelly not portable to
> Windows. I would like to use this test in package check, which can
> include also windows OS.
>
> > On 11-Sep-06 Ted Harding wrote:
> >> 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 Gregor,
> >> 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"
> > else
> > export NETP_UP="NO"
> > fi
> >
> >> 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:
> >>
> >>>>> system("test.inet")
> >>>>> if( sys.getenv(NET_UP") == "YES" ) { ... } else { ... }
> >
> > system("test.inet.sh")
> > 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"
> >> else
> >> export NETTIME=""
> >> fi
> >> if [ "$NETTIME" != "" ] ; then
> >> sleep 1
> >> sleep 1
> >> $NETTIME
> >> /sbin/clock -u -w
> >> date
> >> fi
> >>
> >>
> >> 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.
>
> --
> Lep pozdrav / With regards,
> Gregor Gorjanc
>
> ----------------------------------------------------------------------
> University of Ljubljana PhD student
> Biotechnical Faculty
> Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan
> Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si
>
> SI-1230 Domzale tel: +386 (0)1 72 17 861
> Slovenia, Europe fax: +386 (0)1 72 17 888
>
> ----------------------------------------------------------------------
> "One must learn by doing the thing; for though you think you know it,
> you have no certainty until you try." Sophocles ~ 450 B.C.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list