Ping a website in R
Asked Answered
G

5

18

I would like to create a script in R that pings a given website. I haven't found any information about this specific for R.

To start with, all I need is the information on whether the website responses to the ping or not.

Does anyone have information about existing scripts or which package best to use to start with?

Gerek answered 10/8, 2011 at 14:50 Comment(2)
A comment on terminology: pinging is directed to a host, not a website. If it matters, you may want to understand the difference between a server, host, IP, domain, and website. It isn't a big deal for most purposes, though.Cower
Note that some people construe "ping a website" to mean "send a GET query and make sure a response code 200 is returned". I agree it's not the common terminology but it is out there.Osher
I
23

We can use a system2 call to get the return status of the ping command in the shell. On Windows (and probably linux) following will work :

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

# example
> ping("google.com")
[1] FALSE
> ping("ugent.be")
[1] TRUE

If you want to capture the output of ping, you can either set stdout = "", or use a system call:

> X <- system("ping ugent.be", intern = TRUE)
> X
 [1] ""                                                         "Pinging ugent.be [157.193.43.50] with 32 bytes of data:" 
 [3] "Reply from 157.193.43.50: bytes=32 time<1ms TTL=62"       "Reply from 157.193.43.50: bytes=32 time<1ms TTL=62"      
 [5] "Reply from 157.193.43.50: bytes=32 time<1ms TTL=62"       "Reply from 157.193.43.50: bytes=32 time<1ms TTL=62"      
 [7] ""                                                         "Ping statistics for 157.193.43.50:"                      
 [9] "    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)," "Approximate round trip times in milli-seconds:"          
[11] "    Minimum = 0ms, Maximum = 0ms, Average = 0ms"         

using the option intern = TRUE makes it possible to save the output in a vector. I leave it to the reader as an exercise to rearrange this in order to get some decent output.

Interphone answered 10/8, 2011 at 15:32 Comment(3)
Thanks (upvoted) but it's looping. In your code we have to use ping -c1 (or a finite number). system2("ping", paste0("-c1 ",x),. link: Ping for 4 timesForbid
It works on Mac too. Its just that when the ping is good (the host is available), the function doesn't immediately return the TRUE boolean value. However, when the ping is bad (the host is unavailable), the function immediately returns the FALSE boolean value.Wedded
The suggestion by phili_b solves the issue I mentioned about the function not returning immediately the value TRUE.Wedded
C
12

RCurl::url.exists works for localhost (where ping doesn't always) and is faster than RCurl::getURL.

> library(RCurl)
> url.exists("google.com")
[1] TRUE
> url.exists("localhost:8888")
[1] TRUE
> url.exists("localhost:8012")
[1] FALSE

Note that it is possible to set the timeout (which by default is rather long)

> url.exists("google.com", timeout = 5) # timeout in seconds
[1] TRUE
Christo answered 2/8, 2013 at 19:25 Comment(0)
S
3

To get the status code

library(httr)

b <- GET("http://www.google.com")

b$status_code

[1] 200 
Slush answered 24/10, 2018 at 10:33 Comment(0)
A
3

There is a package for this ... {pingr} Link to CRAN.

library(pingr)

# check if domain can be reached via port 80
is_up(destination = "example.com")

## [1] TRUE


# check how domain name is resolved to ip adress
nsl("example.com")

## $answer
##          name class type   ttl          data
## 1 example.com     1    1 85619 93.184.216.34
## 
## $flags
## aa tc rd ra ad cd 
## NA NA NA NA NA NA 


# check HTTP port
pingr::ping_port("example.com", 80)


# check HTTPS port
pingr::ping_port("example.com", 443)


Andre answered 20/3, 2021 at 20:6 Comment(0)
O
2

If you want to see if a website is responding to HTTP requests, you can test an URL in R with the RCurl library, which is an R interface to the curl HTTP client library.

Example:

> library(RCurl);
> getURL("http://www.google.com")
[1] "<!doctype html><ht....

If you want to examine the response code (for 200, 404, etc.) you will need to write a custom function to pass as the "header" option to getURL().

Osher answered 11/8, 2011 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.