Skip to content

Instantly share code, notes, and snippets.

@goocey
Created June 20, 2013 15:28
Show Gist options
  • Save goocey/5823754 to your computer and use it in GitHub Desktop.
Save goocey/5823754 to your computer and use it in GitHub Desktop.
なんかめんどいので、normalだけ動作確認せずです。
# -*- 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