Code Monkey home page Code Monkey logo

net-ping's Introduction

net-ping

A collection of classes that provide different ways to ping computers.

Prerequisites

  • ffi
  • win32-security (MS Windows only)
  • fakeweb (test only)
  • test-unit (test only)

Ruby users should use Ruby 1.9.3 or later. JRuby users should use JRuby 1.6.7 or later.

Installation

gem install net-ping

Notes

Please read the documentation under the 'doc' directory. Especially pay attention to the documentation pertaining to ECONNREFUSED and TCP pings.

Also note the documentation regarding down hosts.

How to require net-ping

You can do either this:

require 'net/ping'

In which case you will get Net::Ping and all of its subclasses. Or, you can load individual subclasses like this:

require 'net/ping/tcp'

The former has the advantage of being easier to remember and all inclusive, not to mention backwards compatible. The latter has the advantage of reducing your memory footprint.

Known Issues

Older versions of Ruby 1.9.x may not work with UDP pings.

Older versions of JRuby will return false positives in UDP pings because of an incorrect error class being raised. See JRuby-4896.

JRuby 1.6.7 or later is required for external pings because of a bug in earlier versions with open3 and stream handling.

ICMP pings will not work with JRuby without some sort of third-party library support for raw sockets in Java, such as RockSaw.

License

Artistic 2.0

Contributions

Although this library is free, please consider having your company set up a gittip if used by your company professionally.

http://www.gittip.com/djberg96/

More documentation

If you installed this library via Rubygems, you can view the inline documentation via ri or fire up 'gem server', and point your browser at http://localhost:8808.

Author

Daniel J. Berger

net-ping's People

Contributors

aortbals avatar bish0polis avatar chernesk avatar djberg96 avatar eitoball avatar emmatoday avatar epitron avatar grandynguyen avatar j15e avatar lzap avatar migeorge avatar otzy007 avatar pythonesque avatar questiondriven avatar ralfherzog avatar shockwavenn avatar stillhart avatar tardate avatar tosch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

net-ping's Issues

More relevant duration

If end_time calculate a little earlier, the duration would be more relevant.

# lib/net/ping/external.rb
...
Open3.popen3(*pcmd) do |stdin, stdout, stderr, thread|
  stdin.close
  @end_time = Time.now # <-------
  err = stderr.gets # Can't chomp yet, might be nil
...
# There is no duration if the ping failed
@duration = @end_time - start_time if bool
...

What it can be fraught with?
(What negative consequences can be?)

is this library nonblocking?

I have list of several thousand machines I need to ping(all on my company network so nothing shady!). Anyway, to speed this up I was trying to run 50 threads in parallel that would ping these nodes but it appears to me that this gem is traversing over itself when I do that.

Here is an example of how I am doing that:

data_queue= queue.new()
consumer = Array.new(50).each do |num|
  Thread.new do
    unless if queue.empty?
       temp = queue.shift
       data_queue << [temp, Net::Ping::TCP.new(Temp, 'http')]
    end
  end
end
consumer.each(&:join)

data_hash = Array.new()
loop do
  break if data_queue.empty?
  temp = data_queue.shift
  data_hash.merge!(temp[0] => temp[1])
end

Anyway, I'm giving it a list of 5000 machines but am only getting 50 unless I'm just confused as to how memory works in threading I think it has to do with this library not being thread safe.

IPv6 support?

Maybe, we can set pcmd to ping6 somehow in external ping?
or two methods ping and ping6

Plans to use DGRAM sockets for ICMP ?

Hi,

I'm running net-ping gem on macOS High Sierra, and at least Net::Ping::TCP and Net::Ping::External are working ok (the ones I tested).

I tried Net::Ping::ICMP. I see it requires cap2 gem, so it fails with:

Caused by:
LoadError: cannot load such file -- cap2

cap2 is used to check whether there's net_raw enabled or not, because Net::Ping::ICMP uses raw socket.

Why does it use raw sockets? Is there any plan to use DGRAM sockets? Apple's ping utility uses DGRAM when the user is not root.

this gem only works on Linux?

I installed it and started building a "Network ping sweeping", but as soon as I run my program I got this issue:

$ ruby network_ping_sweeping.rb 66.220.144.0
Traceback (most recent call last):
  3: from network_ping_sweeping.rb:6:in `<main>'
  2: from network_ping_sweeping.rb:6:in `new'
  1: from /Users/victor/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/net-ping-2.0.2/lib/net/ping/icmp.rb:31:in `initialize'
/Users/victor/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/net-ping-2.0.2/lib/net/ping/icmp.rb:43:in 
`rescue in initialize': undefined method `StandardError' for #<Net::Ping::ICMP:0x00007f90b589e688> (NoMethodError)

It seems like I need to install the cap2 gem which only runs on Linux.

2.0.3 tag on GitHub

On RubyGem, the gem version is 2.0.3โ€ฆ However, the latest version here on GitHub is 2.0.2.

Maybe tags have not been pushed to GitHub?

ICMP ping should check for either root user OR cap_net_raw

You are allowed to generate raw ICMP packets as a non-root user if the process has been assigned the capability cap_net_raw. The permission check for ICMP needs to take this into account. Otherwise ruby cannot run as a non-root user and use this module.

The extra check needs to go here:
https://github.com/chernesk/net-ping/blob/c54ac6a6109f937e130dd2fed8a463154cac60fd/lib/net/ping/icmp.rb#L31

You could use the module here (link goes to example of process capability query in readme):
https://github.com/lmars/cap2#querying-capabilities

I have not tested that module though. It may "just work" or it maybe be broken as it hasn't had any commits in 5 years...

Or someone could implement the same themselves.

Busybox external ping fails because -i argument doesn't exist

For example on alpine linux with no iputils package installed the ping command doesn't have -i

ping -i
ping: unrecognized option: i
BusyBox v1.24.2 (2016-11-01 12:56:05 GMT) multi-call binary.

Usage: ping [OPTIONS] HOST

Send ICMP ECHO_REQUEST packets to network hosts

	-4,-6		Force IP or IPv6 name resolution
	-c CNT		Send only CNT pings
	-s SIZE		Send SIZE data bytes in packets (default:56)
	-t TTL		Set TTL
	-I IFACE/IP	Use interface or IP address as source
	-W SEC		Seconds to wait for the first response (default:10)
			(after all -c CNT packets are sent)
	-w SEC		Seconds until ping exits (default:infinite)
			(can exit earlier with -c CNT)
	-q		Quiet, only display output at start
			and when finished
	-p		Pattern to use for payload

Not threadsafe?

I've run into problems using Net::Ping::External with threads.

  Thread.new host do |host|
    # Get relevant variables
    host_name = host["host_name"]
    metric_name = host["metric_name"]

    # Ping the host, with a timeout of 5 seconds
    pinger = Net::Ping::External.new(host_name)
    pinger.timeout = 5
    success = pinger.ping
    if success
      time_taken = pinger.duration
    else
      time_taken = pinger.timeout
    end

    # Record the time taken in statsd
    puts "#{host_name}: #{time_taken}"
  end

In this example, I create two threads, to ping 2 different hosts:

10.11.1.1: 0.007786
10.11.1.2: 0.007786
*10 second pause I added*
10.11.1.1: 
10.11.1.2: 0.018783
*10 second pause I added*
10.11.1.1: 0.014204
10.11.1.2: 0.014204

It seems that some data structure is being shared by the 2 threads.

External ping command fails on Void Linux (`-i` argument should come before host?)

I don't exactly know the details; just reporting on behalf of @emacsomancer, who filed an issue on a project of mine, arising from this library's External#ping command.

From his post:

I'm on Linux (Void Linux; using libressl instead of openssl, and musl rather than glibc, if that makes a difference).

If I run:

ping -c 1 -W 5 imap.gmail.com -i 1

the output is:

ping: unknown host -i

(though running ping -c 1 -W 5 imap.gmail.com, produces:

PING gmail-imap.l.google.com (209.85.200.109) 56(84) bytes of data.
64 bytes from jl-in-f109.1e100.net (209.85.200.109): icmp_seq=1 ttl=44 time=120 ms

--- gmail-imap.l.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 120.149/120.149/120.149/0.000 ms

)

$ echo $?
2

False ping6 parameters

I use this gem on freebsd. The -t option is not for setting the timeout. Please create an extra match for /freebsd*/ with -x for setting the timeout.

Windows gem dependency isn't properly set

This gem requires win32-security on Windows hosts, but that's not properly defined in the gemspec. Since the gemspec is parsed by rubygems and uploaded the logic to include that windows depedencies on windows is only included when the gem is uploaded from a windows host. Instead you want to define a universal-mingw32 gemspec that adds additional dependencies on Windows. Then you push both platforms of gems via the two gemspec and windows hosts automatically pull the correct platform with the additional dependency. An example of doing this is the chef gem which includes two gemspecs:

https://github.com/chef/chef/blob/master/chef.gemspec
https://github.com/chef/chef/blob/master/chef-universal-mingw32.gemspec

and produces different gem platforms on rubygems:

https://rubygems.org/gems/chef

Largely unmaintained

I am finding myself unable, with family and work schedules, to maintain this gem. While I would love to be able to, it's not fair to the community for me to take months sometimes to release fixes.

Please contact me at [email protected] or on here if you would like to maintain this gem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.