rcheckip.rb 生存IPリスト取得

前に作った生存しているIPアドレスのリストを取得するスクリプト
取得できるのはx.x.x.0〜x.x.x.255まで。
あまり検索範囲を広げるのも問題なので範囲はこれで十分。

require "ping"

while
        arg = ARGV.shift
        exit if !arg
        get_ip = arg.split(/\./)

        list = []
        for num in 0..255
                site ="#{get_ip[0]}.#{get_ip[1]}.#{get_ip[2]}.#{num}"
                Thread.start(site) do |ip|
                        begin
                        ret = Ping.pingecho(ip, 0.5)
                        ip = ip.split(/\./)
                        list << sprintf("%03d.%03d.%03d.%03d", ip[0], ip[1], ip[2], ip[3]) if ret
                        rescue
                        end
                end
        end

        puts list.sort
end


使用例:

$ rcheckip.rb 192.168.0
192.168.000.001
192.168.000.002