我正在考虑在macOS上使用pingr::ping
函数来ping某些目的地。如果目的地格式错误,我想隐藏pingr::ping
输出。
pingr::ping
实际上利用pingr::ping_os
函数来组装命令,而利用system
命令来执行ping。在macOS上,格式错误的目标在ping
中返回,返回有关格式错误的命令的消息。我想隐藏该消息,使其不被打印到控制台。hide_ping_output(destination = "www.google.com") -> a
hide_ping_output(destination = "wrong destination") -> b
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize] [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout][-W waittime] [-z tos] host ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime] [-z tos] mcast-group Apple specific options (to be specified before mcast-group or host like all options) -b boundif # bind the socket to the interface -k traffic_class # set traffic class socket option -K net_service_type # set traffic class socket options -apple-connect # call connect(2) in the socket -apple-time # display current time [1] NA NA NA
如果目的地格式错误,则不会打印系统输出。
hide_ping_output(destination = "www.google.com")
hide_ping_output(destination = "wrong destination")
a; b
[1] 190.027 36.846 35.243
[1] NA NA NA
sink()
hide_ping_output_sink <- function(...) {
sink(tempfile())
pingr::ping(...)
sink(NULL)
}
hide_ping_output_sink(destination = "wrong destination") -> b
出现不需要的控制台输出。
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize] [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout][-W waittime] [-z tos] host ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime] [-z tos] mcast-group Apple specific options (to be specified before mcast-group or host like all options) -b boundif # bind the socket to the interface -k traffic_class # set traffic class socket option -K net_service_type # set traffic class socket options -apple-connect # call connect(2) in the socket -apple-time # display current time
capture.output
/ invisible
hide_ping_output_capture <- function(...) {
capture.output(invisible(pingr::ping(...) ->> b))
b
}
hide_ping_output_capture(destination = "wrong destination") -> b
出现不需要的控制台输出。
>> hide_ping_output_capture(destination = "wrong destination") -> b usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize] [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout][-W waittime] [-z tos] host ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime] [-z tos] mcast-group Apple specific options (to be specified before mcast-group or host like all options) -b boundif # bind the socket to the interface -k traffic_class # set traffic class socket option -K net_service_type # set traffic class socket options -apple-connect # call connect(2) in the socket -apple-time # display current time
答案 0 :(得分:1)
这可能会增加您的问题,但这是避免该问题的一种方法:
> library(iptools)
> library(pingr)
> hn <- "www.google.com"
> if (hostname_to_ip(hn) != "Not resolved") { ping(hn) }
[1] 617.094 610.771 610.603
> hn <- "foo bar"
> if (hostname_to_ip(hn) != "Not resolved") { ping(hn) }
>
hostname_to_ip()
可能需要花费很长时间才能失败,因此可能首先过滤掉明显的不良主机。
答案 1 :(得分:1)
如果创建了系统消息,我找不到转移系统消息的方法。它们似乎不是来自R的消息流。
我能找到的最佳解决方案是修改ping
:
hide_ping_output <- function(...) {
f <- pingr::ping
body(f)[[4]] <- quote(output <- suppressWarnings(system(os$cmd,
intern = !verbose, ignore.stderr = TRUE)))
f(...)
}