Method for finding available devices from 255 ip addresses

I have 6 devices configured by IP address from 1 to 255 in the range 192.168.1.X (where X = from 1 to 255). I wrote this program for ping and see the available IP address for operations. But accepting it so long to execute ... can anyone suggest a quick way to accomplish this?

Using a plug is also appreciated ...

Here is the program:

server = "192.168.1" for i in (1...255) system("ping -q -c #{timeout} #{server}.#{i} 2&>/dev/null") if $?.exitstatus == 0 # operations end end 
+4
source share
1 answer

Tested with ruby ​​1.9.3 time is not bad;

 [ slmn@uriel ~]$ time ruby ipmap.rb "192.168.0.1" "192.168.0.10" real 0m2.393s user 0m0.750s sys 0m1.547s 

I commented on areas if you want your operations to be streamed;

 require 'ipaddr' ips = IPAddr.new("192.168.1.0/24").to_range threads = ips.map do |ip| Thread.new do status = system("ping -q -W 1 -c 1 #{ip}", [:err, :out] => "/dev/null") # you can do your operations in thread like this # if status # # operations # end # optional Thread.current[:result] = ip.to_s if status end end threads.each {|t| t.join} # if you don't do your operations in thread threads.each do |t| next unless t[:result] # operations #optional puts t[:result] end 
+2
source

Source: https://habr.com/ru/post/1398843/


All Articles