Neil Neil - 3 months ago 19
R Question

How to get a ping response in R

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.

Answer

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.

Comments