Created
June 20, 2013 15:28
-
-
Save g00cey/5823754 to your computer and use it in GitHub Desktop.
なんかめんどいので、normalだけ動作確認せずです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#https://raw.github.com/tluna/ruby/master/network/ping.rb | |
require 'rubygems' | |
require 'net/ping' | |
require 'parallel' | |
require 'benchmark' | |
data = `ifconfig` | |
#すべてのNICからIPアドレス取得 | |
networks = nil; | |
no = 1 | |
networks = Array.new(0,nil) | |
data.each_line do |line| | |
if line =~ /inet ([\d\.]+)/ then | |
networks << $1 | |
end | |
end | |
#IPADDRをとってくる。 | |
ipnet = Array.new() | |
i = 0 | |
networks.each do |network| | |
# loopback以外は取得 | |
if network != '127.0.0.1' then | |
ipnet << network | |
end | |
end | |
#ネットワークを表示 | |
puts 'your networks' | |
ipnet.each do |ipaddr| | |
puts i.to_s + ':' + ipaddr | |
end | |
#pingオブジェクト準備 | |
ping = Net::Ping::External.new() | |
puts "Choose your network" | |
#cnetno = STDIN.gets | |
cnetno = '0' | |
cnet = ipnet[cnetno.to_i] | |
# networkadress抽出 | |
if cnet =~ /([\d.]{3,})\d+$/ then | |
netaddr = $1 | |
end | |
targetaddr = Array.new() | |
for i in 0..255 do | |
targetaddr << netaddr + i.to_s | |
end | |
Benchmark.bm do |x| | |
x.report('normal') { | |
targetaddr.each do |host| | |
if ping.ping?(host) then | |
puts host | |
end | |
end | |
} | |
x.report('thread') { | |
results = Parallel.map(targetaddr, :in_threads => 10) do |host| | |
#ping送信 | |
if ping.ping?(host) then | |
puts host | |
end | |
end | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment