2

I have a dataframe which has domain name column. I want to see which domain exist by pinging their domain names. I can get an individual ping response from following function.

ping <- function(x,stderr=FALSE,stdout=FALSE,...){
pingvec <- system2("ping",x,
                 stderr=FALSE,
                 stdout=FALSE,...)
if (pingvec == 0) TRUE else FALSE
}



ping("google.com")
[1] TRUE

Is there any R package where I can get ping response for entire column which has thousands of domain names.

Neil
  • 7,937
  • 22
  • 87
  • 145

2 Answers2

6

It is not a good idea to use system() commands - as a general rule in any programming language. The main reasons are security and portability issues.

The pingr package provides a possibility to ping a remote server within R:

library(pingr)
URLs <- c("google.com", "yahoo.com")  
sapply(URLs, ping)
#     google.com yahoo.com
#[1,]       14.6       171
#[2,]       14.7       171
#[3,]       14.6       171

edit / comment

Looking at the source code of the functions pingr::ping() and pingr:::ping_os() it appears that the package does use a call to system(). Therefore, I think that security concerns remain, since the OS function ping could be redefined to do something completely different and possibly harmful. What the package does seem to resolve are portability problems, since different methods are called depending on the OS.

Community
  • 1
  • 1
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Whether calling `system` is a security problem depends upon the context. If the code is being run on arbitrary machines which you have no control over, then yes, `ping` could be redefined to be something else and there is a security problem. If you want to ping a bunch of addresses from your own machine, that isn't an issue. – Richie Cotton Aug 21 '16 at 07:04
  • Its very slow. Taking lot of time,is there any other way which is faster? And then I want to see which websites does not ping. – Neil Aug 21 '16 at 09:51
  • @Neil The ping timeout can be changed on the system level. In linux systems an option to reduce this time would be `ping -i 0.2`. You could pass this option to pingr by using `URLs <- paste("-i 0.2",URLs)`. This should speed up the function. Don't know about options on other OS. – RHertel Aug 21 '16 at 10:31
  • I meant to say the ping *interval*, not the timeout. – RHertel Aug 21 '16 at 10:45
  • @RHertel How can I use that option in pingr ? – Neil Aug 21 '16 at 15:26
  • @Neil This works on my computer: `URLs <- c("google.com", "yahoo.com") ; URLs <- paste("-i 0.2",URLs); sapply(URLs, ping)`. Essentially, the option `-i 0.2` is attached as a character string at the beginning of each URL. Hope this helps. – RHertel Aug 21 '16 at 15:38
  • @RHertel When I run above code it gives me all NA values `-i 0.2 google.com -i 0.2 yahoo.com [1,] NA NA [2,] NA NA [3,] NA NA` – Neil Aug 21 '16 at 15:44
  • @Neil I'm afraid I can't help any further. There's probably a reason why the default interval is 1s. Maybe try to increase the value (0.2s is the smallest value allowed for non-superusers on my system, which is Ubuntu 16.04). Note that the URLs need to be defined again. You could try, e.g. `URLs <- c("google.com", "yahoo.com"); sapply(paste("-i", interval=0.4, URLs), ping)`. – RHertel Aug 21 '16 at 16:07
  • @RHertel Still does not work. Thanks for your help and time. Appreciated – Neil Aug 21 '16 at 16:25
2

We can wrap with Vectorize and do for multiple elements

ping1 <- Vectorize(ping)
ping1(c("yahoo.com", "google.com"))
ping1("google.com")
akrun
  • 874,273
  • 37
  • 540
  • 662