Ping with ruby

How can I do a script in ruby that to do a ping.
Es. I want ping 192.168.1.1.
How I do it ??
Thank you.
For telnet and ftp I have found how to do, but no for ping.
Thank you

Panther wrote:

How can I do a script in ruby that to do a ping.

http://www.ruby-lang.org/raa/list.rhtml?name=net-pingsimple

For telnet and ftp I have found how to do, but no for ping.

The following source comes from RAA and it’s modified by me
(to suit my own needs) … enjoy :slight_smile:

#—= begin =---------------------------------------------------------------
#!/usr/bin/env ruby
=begin
Copyright(c) 2001 arton, under GPL2.

== SYNOPSIS
require ‘icmpping’
result = ICMPPing.ping hostname [, timeout(millisecs) [, packetsize(<1024)]]

== Return Value

-1 : Any Socket Error
-2 : Timeout
-3 : Not root

=0: round trip time (in milliseconds)

== Notes
Only Linux now supported
You must be root
Miliseconds now work correctly
Only a class now

=end

require ‘socket’
require ‘timeout’

class ICMPPing
IPPROTO_ICMP = 1
ICMP_ECHO = 8
ICMP_ECHOREPLY = 0
DEF_PACKET_SIZE = 64
MAX_PACKET_SIZE = 1024

def ICMPPing.ping(host, timeout = 1000, dlen = self::DEF_PACKET_SIZE)
begin
raise ArgumentError, “Packet too long” if dlen > MAX_PACKET_SIZE
hp = [Socket::AF_INET, 0, Socket.gethostbyname(host)[3], 0, 0]
s = Socket.new(Socket::AF_INET, Socket::SOCK_RAW, IPPROTO_ICMP)
rescue Errno::EPERM
$stderr.puts “Error: #{$!.message}” if $VERBOSE
return -3
rescue
$stderr.puts “Error: #{$!.message}” if $VERBOSE
return -1
end

start = Time.now.to_f * 1000
id = $$ & 0xffff
icmph = [ ICMP_ECHO, 0, 0, id, 0, start.to_i & 0xffffffff, nil ]
icmph[6] = "E" * dlen
dat = icmph.pack("C2n3Na*")
cksum = checksum(((dat.length & 1) ? (dat + "\0") : dat).unpack("n*"))
dat[2], dat[3] = cksum >> 8, cksum & 0xff
begin
  s.send dat, 0, hp.pack("v2a*N2")
  timeout(timeout / 1000.0) do
    while true
      rdat = s.recvfrom(self::MAX_PACKET_SIZE + 500)
      icmpdat = rdat[0].slice((rdat[0][0] & 0x0f) * 4..-1)
      resp = icmpdat.unpack("C2n3N")
      next if resp[0] != ICMP_ECHOREPLY || resp[3] != id
      break
    end
  end
rescue TimeoutError
  $stderr.puts "Error: #{$!.message}" if $VERBOSE
  return -2
rescue
  $stderr.puts "Error: #{$!.message}" if $VERBOSE
  return -1
end

Time.now.to_f * 1000 - start

end

def ICMPPing.checksum(n)
ck = 0
n.each{ |v| ck += v }
ck = (ck >> 16) + (ck & 0xffff)
ck += ck >> 16
~ck
end
end

if $0 == FILE
if ARGV.empty?
print “usage: icmpping.rb [timeout(ms=2000)]\n”
exit 1
end
$VERBOSE = true
rd = ICMPPing.ping(ARGV[0], (ARGV[1] || “2000”).to_i)
if rd < 0
exit rd.abs
else
puts “#{ICMPPing::DEF_PACKET_SIZE} bytes, rtt: #{rd} msec”
end
end
#—= end =---------------------------------------------------------------

W.

···

Modifications and fixes 2003 Wejn wejn@box.cz

          Wejn <lists+rubytalk(at)box.cz>

(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

I have installed it but with this script I have this error:
Error
pin.rb:5: parse error
t = Net::PingTCP.new(192.168.1.1

Script
require ‘net/pingsimple’

check with udp or tcp ping

t = Net::PingTCP.new(192.168.1.1)
#u = Net::PingUDP.new(host)
#e = Net::PingExternal.new(host)

if t.ping
puts “Host is alive”
unless t.warning.nil?
puts "Warning encountered: " + t.warning
end
else
puts "Couldn’t connect to host: " + t.exception
end

Please can you say me how to write a scrip correct that to do a ping to
192.168.1.1 ???
Thank you

···

On Tue, 28 Jan 2003 14:43:54 +0000, Lyle Johnson wrote:

Panther wrote:

How can I do a script in ruby that to do a ping.

http://www.ruby-lang.org/raa/list.rhtml?name=net-pingsimple

…or with:
http://raa.ruby-lang.org/list.rhtml?name=icmpmodule

···

On Wed, Jan 29, 2003 at 05:43:11AM +0900, Lyle Johnson wrote:

How can I do a script in ruby that to do a ping.
http://www.ruby-lang.org/raa/list.rhtml?name=net-pingsimple

gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/
[ “Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej.” ]

That’s not a string, use “192.168.1.1”

···

Panther (saspurss@libero.it) wrote:

I have installed it but with this script I have this error:

pin.rb:5: parse error
t = Net::PingTCP.new(192.168.1.1


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

That’s not a string, use “192.168.1.1”
It is not good, I have this error if I use “192.168.1.1”

Couldn’t connect to host: Connection refused - “connect(2)”

That’s not a string, use “192.168.1.1”
It is not good, I have this error if I use “192.168.1.1”

Couldn’t connect to host: Connection refused - “connect(2)”

Actually, this is good. It means that you are actually making ICMP
(ping) requests.

-pate

···

On Wed, 29 Jan 2003, Panther wrote:

if by my pc I ping my ip address (192.168.1.1), it’s ok.
If i to do this by script ruby I have this error,
It is not correct?? What is script correct ??

Hi,

“Panther” saspurss@libero.it writes:

if by my pc I ping my ip address (192.168.1.1), it’s ok.
If i to do this by script ruby I have this error,
It is not correct?? What is script correct ??

The README says:
FAQ

···

===
“Why don’t you return exceptions if a connection fails?” - Because ping is
only meant to return one of two things - success or failure. It’s very
simple. If you want to find out why the ping failed, you can check the
‘exception’ attribute.

“I know the host is alive, but a TCP or UDP ping tells me otherwise. What
gives?” - It’s possible that the echo port has been disabled on the remote
host for security reasons. Your best best is to specify a different port
or to use PingExternal instead.


eban

Ok script is ok if I use Net::PingExternal.new.
When ip address that I put is live script is ok, but when I put into
script a ip address wrong I don’t know how to set a timeout.
I put timeout:
pe = Net::PingExternal.new(“192.168.1.2”, timeout=“3”)
but it don’t go, ping go go and I don’t know how I can stop it. How to set
a timeout, so I have a string that say me (ip address is not valid or I
can’t connect to it!!).
TCP ping unsuccessful: Connection refused - "connect(2)"
UDP ping unsuccessful: Connection refused - "recvfrom(2)"
TCP adn UDP say me that are not connect but PingExternal is ok. Isn’t
strange ???
Thank you