本文介绍了如何在 Ruby 中 ping 远程主机的可达性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

Ping.pingecho("10.102.52.42", 30)

用于远程主机的可达性.即使我能够手动 ping IP,此语句也会返回 null.有没有一种有效的方法可以在 Ruby 中确定远程机器的可达性?

for reachability of the remote host. This statement returns null even if I'm able to ping the IP manually. Is there an efficient way to determine the reachability of the remote machine in Ruby?

推荐答案

我使用您需要安装的 net-ping gem.然后代码很简单:

I use the net-ping gem which you need to install. Then the code is easy:

#!/usr/bin/env ruby

require 'net/ping'

def up?(host)
    check = Net::Ping::External.new(host)
    check.ping?
end

chost = '10.0.0.1'
puts up?(chost) # prints "true" if ping replies

这篇关于如何在 Ruby 中 ping 远程主机的可达性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 06:02