匹配ping的某个方面

时间:2016-02-09 14:45:26

标签: ruby ping

我正在编写一个必须ping打印机的程序,但我不喜欢ping信息,我觉得它看起来很草率:

PING laser32 (10.208.7.13) 56(84) bytes of data.
64 bytes from laser32 (10.208.7.13): icmp_seq=1 ttl=62 time=0.837 ms

--- laser32 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.837/0.837/0.837/0.000 ms

有没有办法可以匹配这个1 packets transmitted并摆脱其余的?

到目前为止我尝试的是:

  • [/1 packets transmitted/]
  • gsub< =完全没有帮助

来源:

def ping
  send = system("ping -c 1 #{@printer}")
  send.match[/1 packets transmitted/]
  puts send
  # case
  # when send != "1 packets transmitted, 1 received, 0% packet loss"
  #   send_print_jobs
  # else
  #   raise LoadError.new("Printer: #{@printer} failed to ping, check network connection")
  # end
end

1 个答案:

答案 0 :(得分:1)

Kernel#system返回true/false。要接收命令输出,请使用backticks or %x

`ping -c 1 #{@printer}`[/1 packets transmitted/]
#⇒ 1 packets transmitted