【问题标题】:Hiding output of function that calls system command隐藏调用系统命令的函数的输出
【发布时间】: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


【解决方案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(...)
}

【讨论】:

  • 非常感谢您对此进行调查。我遇到了同样的问题,pingr 输出似乎逃脱了 sink 或 capture.output。
【解决方案2】:

这可能会增加您的问题,但这里有一个回避问题的方法:

> 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() 可能需要很长时间才能失败,因此可能先过滤明显的不良主机。

【讨论】:

  • 感谢您提供部分解决方案。如果没有其他出现,我会接受。我很想了解更多关于sinkcapture.output 的信息,看看是否有人可以提出实际捕获所有内容 的解决方案,包括ping 可能返回的任何错误消息。如果没有类似的情况出现,我会接受你的解决方案。
  • 是的 - 对不起!我不太了解这些是如何工作的。我走的是与你相同的道路,结果(自然)相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2012-06-10
  • 2015-05-14
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多