隐藏调用系统命令的函数输出

问题描述 投票:3回答:2

Background

我正在考虑在macOS上使用pingr::ping函数来ping某些目的地。我想隐藏pingr::ping输出,以防错误格式化目的地。

笔记

  • pingr::ping实际上利用pingr::ping_os函数来汇编命令和system命令来执行ping。在macOS上,格式错误的目标在ping中返回,返回有关格式错误的命令的消息。我希望隐藏该消息不被打印到控制台。

Example

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

Desired results

如果格式错误的目的地,则不会打印系统输出。

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

Attempts

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
r macos function ping stderr
2个回答
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(...)
}

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

© www.soinside.com 2019 - 2024. All rights reserved.