【发布时间】:2019-03-25 07:24:02
【问题描述】:
背景
我正在考虑在 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
【问题讨论】:
-
你能改正
pingr中的错字吗? -
你应该用你的操作系统标记它。我在 Win10 上没有看到这样的输出。
-
@Roland 当然,谢谢。
标签: r macos function ping stderr