ruby ping for 1.9.1

前端 未结 4 1045
萌比男神i
萌比男神i 2021-02-03 10:37

I want to ping a site in my ruby code and saw that net-ping was a nice library to do this with. Unfortunately, when I tried to gem install net-ping I got the following error:

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 10:43

    You can always do this and use regexps to parse the result or just check the exit status:

    ping_count = 10
    server = "www.google.com"
    result = `ping -q -c #{ping_count} #{server}`
    if ($?.exitstatus == 0) do
      puts "Device is up!"
    end
    

    Ping return values that you can check against:

    The ping utility returns an exit status of zero if at least one response was heard from the specified host; a status of two if the transmission was successful but no responses were received; or another value (from ) if an error occurred.

    http://www.manpagez.com/man/8/ping

提交回复
热议问题