Skip to content

Instantly share code, notes, and snippets.

@rtlong
Created June 10, 2012 23:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtlong/2907619 to your computer and use it in GitHub Desktop.
Save rtlong/2907619 to your computer and use it in GitHub Desktop.
Ruby ping scan
finished = []
ips = []
range = 1..254
range.each do |i|
ip_address = "192.168.111.#{i}"
Thread.new do
`ping -q -c1 -W1 #{ip_address}`.split(/\n+/)[2].split(/,\s*/)[1].to_i > 0 and ips << ip_address
finished << i
end
end
until finished.count == range.count
print "\r%#{(finished.count/range.count.to_f * 100).round}"
sleep 0.1
end
print "\r \r"
puts ips.sort do |a, b|
a.split(?.)[-1].to_i <=> b.split(?.)[-1].to_i
end
finished = []; ips = []; range = 1..254; range.each { |i| ip_address = "192.168.111.#{i}"; Thread.new { `ping -q -c1 -W1 #{ip_address}`.split(/\n+/)[2].split(/,\s*/)[1].to_i > 0 and ips << ip_address; finished << i } }; until finished.count == range.count; print "\r%#{(finished.count/range.count.to_f * 100).round}"; sleep 0.1; end; print "\r \r"; puts ips.sort { |a, b| a.split(?.)[-1].to_i <=> b.split(?.)[-1].to_i }
@rtlong
Copy link
Author

rtlong commented Jun 10, 2012

This was tested only on MRI Ruby 1.9.3-p194. You may need to tweak the timeout or counts, depending on the nature and quality of the network you're testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment