【问题标题】:Ping a website in R在 R 中 Ping 一个网站
【发布时间】:2011-10-24 04:14:27
【问题描述】:

我想在 R 中创建一个 ping 给定网站的脚本。我还没有找到任何关于这个特定于 R 的信息。

首先,我需要的只是网站是否响应 ping 的信息。

有没有人知道关于现有脚本或最适合从哪个包开始的信息?

【问题讨论】:

  • 对术语的评论:ping 指向主机,而不是网站。如果重要,您可能想了解服务器、主机、IP、域和网站之间的区别。不过,对于大多数用途来说,这没什么大不了的。
  • 请注意,有些人将“ping 网站”解释为“发送 GET 查询并确保返回响应代码 200”。我同意这不是通用术语,但它就在那里。

标签: r ping


【解决方案1】:

我们可以使用system2 调用来获取shell 中ping 命令的返回状态。在 Windows(可能还有 linux)上,以下将起作用:

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

如果要捕获ping的输出,可以设置stdout = "",或者使用系统调用:

> 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"         

使用选项intern = TRUE 可以将输出保存在向量中。我把它留给读者作为练习,重新排列它以获得一些不错的输出。

【讨论】:

  • 谢谢(赞成),但它正在循环播放。在您的代码中,我们必须使用ping -c1(或有限数)。 system2("ping", paste0("-c1 ",x),。链接:Ping for 4 times
  • 它也适用于 Mac。只是当 ping 良好(主机可用)时,该函数不会立即返回 TRUE 布尔值。但是,当 ping 不正确(主机不可用)时,该函数立即返回 FALSE 布尔值。
【解决方案2】:

如果您想查看网站是否响应 HTTP 请求,您可以使用 RCurl library 在 R 中测试 URL,这是 curl HTTP client library 的 R 接口。

例子:

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

如果您想检查响应代码(对于 200、404 等),您需要编写一个自定义函数作为“header”选项传递给 getURL()。

【讨论】:

    【解决方案3】:

    RCurl::url.exists 适用于 localhost(ping 并不总是如此)并且比 RCurl::getURL 快。

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

    请注意,可以设置超时(默认情况下相当长)

    > url.exists("google.com", timeout = 5) # timeout in seconds
    [1] TRUE
    

    【讨论】:

      【解决方案4】:

      获取状态码

      library(httr)
      
      b <- GET("http://www.google.com")
      
      b$status_code
      
      [1] 200 
      

      【讨论】:

        【解决方案5】:

        这个有一个包... {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)
        
        
        

        【讨论】:

          猜你喜欢
          • 2010-11-12
          • 2011-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-23
          • 2018-01-22
          • 1970-01-01
          相关资源
          最近更新 更多