Class: Snackhack2::WebServerCleaner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: File.join('/var/log', 'access.log')) ⇒ WebServerCleaner

Returns a new instance of WebServerCleaner.



6
7
8
9
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 6

def initialize(path: File.join('/var/log', 'access.log'))
  @ip   = ip
  @path = path
end

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



5
6
7
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 5

def ip
  @ip
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 11

def run
  out = ''
  # generate random IP
  new_ip = Array.new(4) { rand(256) }.join('.')
  File.readlines(@path).each do |line|
    old_ip = line.match(/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/)
    out += if old_ip.to_s == @ip
             line.gsub(old_ip.to_s, new_ip)
           else
             line
           end
  end
  File.delete(@path)
  File.open(@path, 'w+') { |file| file.write(out) }
end