Class: NetworkScanner::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/network_scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Scanner

Returns a new instance of Scanner.



29
30
31
32
# File 'lib/network_scanner.rb', line 29

def initialize(opts = {})
  @pool_size = 100
  @format = 'text'
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



27
28
29
# File 'lib/network_scanner.rb', line 27

def format
  @format
end

#output_nameObject

Returns the value of attribute output_name.



27
28
29
# File 'lib/network_scanner.rb', line 27

def output_name
  @output_name
end

#pool_sizeObject

Returns the value of attribute pool_size.



27
28
29
# File 'lib/network_scanner.rb', line 27

def pool_size
  @pool_size
end

Instance Method Details

#check_hostnamesObject

Raises:

  • (Exception)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/network_scanner.rb', line 195

def check_hostnames
  raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips_to_check

  puts "Scanning for hostnames out of a total of #{@ips_to_check.length} ips"

  if self.output_name
    out = File.open(self.output_name, 'w')
  else
    out = STDOUT
  end

  pool = Thread.pool(@pool_size)

  @ip_hosts = []

  @ips_to_check.each do |ip|
    pool.process do
      scan = `nslookup #{ip}`
      hostname = scan[/name\ =\ (.*)\n/, 1]
      if hostname
        @ip_hosts << [ip, hostname]
        if text?
          out.print "#{hostname} => #{ip}\n"
        end
      end
    end
  end

  pool.shutdown

  if json?
    out.puts(JSON.pretty_generate(@ip_hosts))
  end

  out.close unless out == STDOUT

  return @ip_hosts
end

#check_pingsObject

Raises:

  • (Exception)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/network_scanner.rb', line 112

def check_pings
  raise Exception.new("Must specify ips to check(interface/range/cache)") unless @ips_to_check

  @ips = []

  pool = Thread.pool(@pool_size)

  if self.output_name
    out = File.open(self.output_name, 'w')
  else
    out = STDOUT
  end

  @ips_to_check.each do |ip|
    pool.process do
      # For some reason &> isn't working, so pipe stdout and then stderr
      if system("ping -c 1 #{ip} >> /dev/null 2> /dev/null")
        @ips << ip
        if text?
          out.print "#{ip}\n"
        end
      end
    end
  end

  pool.shutdown

  if json?
    out.puts(JSON.pretty_generate(@ips))
  end

  out.close unless out == STDOUT

  return @ips
end

#check_ports(port) ⇒ Object

Raises:

  • (Exception)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/network_scanner.rb', line 158

def check_ports(port)
  raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips_to_check

  puts "Checking for ports out of a total of #{@ips_to_check.length} ips"

  if self.output_name
    out = File.open(self.output_name, 'w')
  else
    out = STDOUT
  end

  pool = Thread.pool(@pool_size)

  @ips = []

  @ips_to_check.each do |ip|
    pool.process do
      if port_open?(ip, port)
        @ips << ip
        if text?
          out.print "#{ip}\n"
        end
      end
    end
  end

  pool.shutdown

  if json?
    out.puts(JSON.pretty_generate(@ips))
  end

  out.close unless out == STDOUT

  return @ips
end

#get_cache_ips(file) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/network_scanner.rb', line 97

def get_cache_ips(file)
  @ips_to_check = []

  if text?
    File.open(file).each_line do |ip|
      @ips_to_check << ip
    end
  elsif json?
    JSON.parse(File.read(file)).each do |ip|
      @ips_to_check << ip
    end
  end
  return @ips_to_check
end

#get_interface_ips(interface) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/network_scanner.rb', line 42

def get_interface_ips(interface)
  ifconfig = `ifconfig`.split("\n\n").index_by{|x| x[/\w+/,0]}
  inet = ifconfig[interface][/inet addr:([^\s]*)/, 1].split('.')
  broadcast = ifconfig[interface][/Bcast:([^\s]*)/, 1].split('.')
  mask = ifconfig[interface][/Mask:([^\s]*)/, 1].split('.')

  start_first = inet[0].to_i & mask[0].to_i
  start_second = inet[1].to_i & mask[1].to_i
  start_third = inet[2].to_i & mask[2].to_i
  start_fourth = inet[3].to_i & mask[3].to_i

  first_range = start_first..broadcast[0].to_i
  second_range = start_second..broadcast[1].to_i
  third_range = start_third..broadcast[2].to_i
  fourth_range = start_fourth..broadcast[3].to_i
  
  @ips_to_check = []

  first_range.each do |first|
    second_range.each do |second|
      third_range.each do |third|
        fourth_range.each do |fourth|
          @ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
        end
      end
    end
  end

  puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
  return @ips_to_check
end

#get_range_ips(range) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/network_scanner.rb', line 74

def get_range_ips(range)
  start, finish = range.split('-', 2).map{|ip| ip.split('.')}
  first_range = start[0]..finish[0]
  second_range = start[1]..finish[1]
  third_range = start[2]..finish[2]
  fourth_range = start[3]..finish[3]

  @ips_to_check = []

  first_range.each do |first|
    second_range.each do |second|
      third_range.each do |third|
        fourth_range.each do |fourth|
          @ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
        end
      end
    end
  end

  puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
  return @ips_to_check
end

#json?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/network_scanner.rb', line 38

def json?
  @format == 'json'
end

#port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
# File 'lib/network_scanner.rb', line 148

def port_open?(ip, port)
  Timeout::timeout(0.5) do
    s = TCPSocket.new(ip,port)
    s.close
    return true
  end
rescue Timeout::Error
  return false
end

#text?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/network_scanner.rb', line 34

def text?
  @format == 'text'
end