scanGogs.rb
#!/usr/bin/env ruby
require 'net/http'
require 'thread'
require 'english' # config
CONFIG_IPDOMAIN = '30.96.68.'.freeze
CONFIG_RANGE = (2..254)
CONFIG_URITEMPLATE = 'http://%s:3000/user/login'.freeze
CONFIG_READ_TIMEOUT = 30
CONFIG_HIDE_ERROR = true # tty
TTY_FG = { Black: 30, Red: 31, Green: 32, Yellow: 33, Blue: 34,
Magenta: 35, Cyan: 36, White: 37 }.freeze
TTY_BG = { Black: 40, Red: 41, Green: 42, Yellow: 43, Blue: 44,
Magenta: 45, Cyan: 46, White: 47 }.freeze
TTY_MD = { Reset: 0, Bold: 1, Italics: 3, Underlined: 4 }.freeze def tty_msg(msg, tty_fg: nil, tty_bg: nil, tty_md: nil)
l = []
l << TTY_MD[tty_md] if tty_md
l << TTY_FG[tty_fg] if tty_fg
l << TTY_BG[tty_bg] if tty_bg
if l.size > 0
m = l.join(';')
"\033[#{m}m#{msg}\033[0m"
else
msg
end
end # tty styles
def note_style(msg)
tty_msg(msg, tty_fg: :Yellow, tty_md: :Underlined)
end def yes_style(msg)
tty_msg(msg, tty_fg: :Black, tty_md: :Bold, tty_bg: :Green)
end def httpcode_style(msg)
tty_msg(msg, tty_fg: :White, tty_bg: :Blue)
end def error_style(msg)
tty_msg(msg, tty_fg: :White, tty_bg: :Red)
end # timeout
module Net
# initialize timeout
class HTTP
alias old_initialize initialize def initialize(*args)
old_initialize(*args)
@read_timeout = CONFIG_READ_TIMEOUT
end
end
end # main
puts note_style('Working...')
success_list = []
httperr_list = []
threads = []
CONFIG_RANGE.each do |n|
threads << Thread.new do
s = CONFIG_IPDOMAIN + String(n)
uri = URI(format(CONFIG_URITEMPLATE, s))
begin
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
s << "\t" << yes_style('**YES**')
success_list << uri.to_s
else
httperr = "\t#{httpcode_style(res.code)} #{res.message}\n"
s << httperr
httperr_list << (uri.to_s + httperr)
end
puts s
rescue
unless CONFIG_HIDE_ERROR
s << "\t#{error_style('error')} #{$ERROR_INFO}\n"
puts s
end
end
end
end threads.each(&:join)
puts "\n" << note_style("HttpErr Result: #{httperr_list.size}")
httperr_list.each { |s| puts s }
puts "\n" << note_style("Success Result: #{success_list.size}")
success_list.each { |s| puts s }