当前位置: 首页 > 工具软件 > Ruby TTY > 使用案例 >

扫描局域网中Gogs服务器(ruby)

南门鸿哲
2023-12-01

scanGogs.rb

 1 #!/usr/bin/env ruby
 2 require 'net/http'
 3 require 'thread'
 4 require 'english'
 5 
 6 # config
 7 CONFIG_IPDOMAIN = '30.96.68.'.freeze
 8 CONFIG_RANGE = (2..254)
 9 CONFIG_URITEMPLATE = 'http://%s:3000/user/login'.freeze
10 CONFIG_READ_TIMEOUT = 30
11 CONFIG_HIDE_ERROR = true
12 
13 # tty
14 TTY_FG = { Black: 30, Red: 31, Green: 32, Yellow: 33, Blue: 34,
15            Magenta: 35, Cyan: 36, White: 37 }.freeze
16 TTY_BG = { Black: 40, Red: 41, Green: 42, Yellow: 43, Blue: 44,
17            Magenta: 45, Cyan: 46, White: 47 }.freeze
18 TTY_MD = { Reset: 0, Bold: 1, Italics: 3, Underlined: 4 }.freeze
19 
20 def tty_msg(msg, tty_fg: nil, tty_bg: nil, tty_md: nil)
21   l = []
22   l << TTY_MD[tty_md] if tty_md
23   l << TTY_FG[tty_fg] if tty_fg
24   l << TTY_BG[tty_bg] if tty_bg
25   if l.size > 0
26     m = l.join(';')
27     "\033[#{m}m#{msg}\033[0m"
28   else
29     msg
30   end
31 end
32 
33 # tty styles
34 def note_style(msg)
35   tty_msg(msg, tty_fg: :Yellow, tty_md: :Underlined)
36 end
37 
38 def yes_style(msg)
39   tty_msg(msg, tty_fg: :Black, tty_md: :Bold, tty_bg: :Green)
40 end
41 
42 def httpcode_style(msg)
43   tty_msg(msg, tty_fg: :White, tty_bg: :Blue)
44 end
45 
46 def error_style(msg)
47   tty_msg(msg, tty_fg: :White, tty_bg: :Red)
48 end
49 
50 # timeout
51 module Net
52   # initialize timeout
53   class HTTP
54     alias old_initialize initialize
55 
56     def initialize(*args)
57       old_initialize(*args)
58       @read_timeout = CONFIG_READ_TIMEOUT
59     end
60   end
61 end
62 
63 # main
64 puts note_style('Working...')
65 success_list = []
66 httperr_list = []
67 threads = []
68 CONFIG_RANGE.each do |n|
69   threads << Thread.new do
70     s = CONFIG_IPDOMAIN + String(n)
71     uri = URI(format(CONFIG_URITEMPLATE, s))
72     begin
73       res = Net::HTTP.get_response(uri)
74       if res.is_a?(Net::HTTPSuccess)
75         s << "\t" << yes_style('**YES**')
76         success_list << uri.to_s
77       else
78         httperr = "\t#{httpcode_style(res.code)} #{res.message}\n"
79         s << httperr
80         httperr_list << (uri.to_s + httperr)
81       end
82       puts s
83     rescue
84       unless CONFIG_HIDE_ERROR
85         s << "\t#{error_style('error')} #{$ERROR_INFO}\n"
86         puts s
87       end
88     end
89   end
90 end
91 
92 threads.each(&:join)
93 puts "\n" << note_style("HttpErr Result: #{httperr_list.size}")
94 httperr_list.each { |s| puts s }
95 puts "\n" << note_style("Success Result: #{success_list.size}")
96 success_list.each { |s| puts s }

 

转载于:https://www.cnblogs.com/Bob-wei/p/5845053.html

 类似资料: