Tabs with spaces

This commit is contained in:
Will Bradley 2013-09-25 21:45:12 -07:00
parent 530f4834ca
commit 3ddfb3442b

View File

@ -145,111 +145,111 @@ end
def scan def scan
Rails.logger.info "starting scan..." Rails.logger.info "starting scan..."
# Command line arguments # Command line arguments
options = {}; options = {};
OptionParser.new { |opts| OptionParser.new { |opts|
opts.banner = "Usage: pamela-scanner.rb --interface=en0" opts.banner = "Usage: pamela-scanner.rb --interface=en0"
options[:verbose] = true options[:verbose] = true
opts.on("v", "--verbose", "Run verbosely") { |verbose| opts.on("v", "--verbose", "Run verbosely") { |verbose|
options[:verbose] = verbose options[:verbose] = verbose
} }
options[:interface] = "eth0" options[:interface] = "eth0"
opts.on("i", "--interface=interface", "Network Interface") { |interface| opts.on("i", "--interface=interface", "Network Interface") { |interface|
options[:interface] = interface options[:interface] = interface
} }
options[:max_age] = 20 options[:max_age] = 20
opts.on("a", "--max-age=minutes", "Minutes to keep expired macs active") { |max_age| opts.on("a", "--max-age=minutes", "Minutes to keep expired macs active") { |max_age|
options[:max_age] = max_age.to_i options[:max_age] = max_age.to_i
} }
options[:db_host] = "configure_me" options[:db_host] = "configure_me"
opts.on("r", "--db-host=host", "Database Host") { |host| opts.on("r", "--db-host=host", "Database Host") { |host|
options[:db_host] = host options[:db_host] = host
} }
options[:db_name] = "configure_me" options[:db_name] = "configure_me"
opts.on("n", "--db-name=name", "Database Name") { |name| opts.on("n", "--db-name=name", "Database Name") { |name|
options[:db_name] = name options[:db_name] = name
} }
options[:db_user] = "configure_me" options[:db_user] = "configure_me"
opts.on("u", "--db-user=user", "Database User") { |user| opts.on("u", "--db-user=user", "Database User") { |user|
options[:db_user] = user options[:db_user] = user
} }
options[:db_password] = "configure_me" options[:db_password] = "configure_me"
opts.on("p", "--db-password=password", "Database Password") { |password| opts.on("p", "--db-password=password", "Database Password") { |password|
options[:db_password] = password options[:db_password] = password
} }
}.parse! }.parse!
# Open the database # Open the database
#ActiveRecord::Base::establish_connection( #ActiveRecord::Base::establish_connection(
# :adapter => "mysql2", # :adapter => "mysql2",
# :host => options[:db_host], # :host => options[:db_host],
# :database => options[:db_name], # :database => options[:db_name],
# :username => options[:db_user], # :username => options[:db_user],
# :password => options[:db_password]) # :password => options[:db_password])
#class Mac < ActiveRecord::Base #class Mac < ActiveRecord::Base
#end #end
#class MacLog < ActiveRecord::Base #class MacLog < ActiveRecord::Base
#end #end
# Scan the network for mac addresses # Scan the network for mac addresses
macs = {}; macs = {};
command = sprintf("arp-scan -R --interface=%s --localnet", options[:interface]) command = sprintf("arp-scan -R --interface=%s --localnet", options[:interface])
if options[:verbose] if options[:verbose]
Rails.logger.info "Running [#{command}]" Rails.logger.info "Running [#{command}]"
end end
IO.popen(command) { |stdin| IO.popen(command) { |stdin|
Rails.logger.info "Reading stdin: "+stdin.inspect Rails.logger.info "Reading stdin: "+stdin.inspect
stdin.each { |line| stdin.each { |line|
next if line !~ /^([\d\.]+)\s+([[:xdigit:]:]+)\s/; next if line !~ /^([\d\.]+)\s+([[:xdigit:]:]+)\s/;
macs[$2] = $1; macs[$2] = $1;
} }
} }
# Scan the existing macs and update each record as necessary # Scan the existing macs and update each record as necessary
Mac.find(:all).each { |entry| Mac.find(:all).each { |entry|
mac = entry.mac.downcase mac = entry.mac.downcase
ip = entry.ip ip = entry.ip
if macs.has_key?(mac) if macs.has_key?(mac)
if ! entry.active || ! entry.since if ! entry.active || ! entry.since
Rails.logger.info "Activating #{mac} at #{ip}" if options[:verbose] Rails.logger.info "Activating #{mac} at #{ip}" if options[:verbose]
entry.since = Time.now entry.since = Time.now
MacLog.new(:mac => mac, :ip => ip, :action => "activate").save MacLog.new(:mac => mac, :ip => ip, :action => "activate").save
end end
entry.active = 1 entry.active = 1
entry.ip = ip entry.ip = ip
entry.refreshed = Time.now entry.refreshed = Time.now
entry.save entry.save
macs.delete(mac) macs.delete(mac)
next next
end end
# Entry is no longer current # Entry is no longer current
if entry.active if entry.active
ageMinutes = ((Time.now - entry.refreshed)/60).to_i ageMinutes = ((Time.now - entry.refreshed)/60).to_i
next if ageMinutes < options[:max_age] next if ageMinutes < options[:max_age]
Rails.logger.info "Deactivating #{mac}, #{ageMinutes} minutes old" if options[:verbose] Rails.logger.info "Deactivating #{mac}, #{ageMinutes} minutes old" if options[:verbose]
entry.active = 0 entry.active = 0
entry.save entry.save
MacLog.new(:mac => mac, :ip => ip, :action => "deactivate").save MacLog.new(:mac => mac, :ip => ip, :action => "deactivate").save
end end
} }
# Add entries for any macs not already in the db # Add entries for any macs not already in the db
macs.each { |mac, ip| macs.each { |mac, ip|
Rails.logger.info "Activating new entry #{mac} at #{ip}" if options[:verbose] Rails.logger.info "Activating new entry #{mac} at #{ip}" if options[:verbose]
Mac.new(:mac => mac, :ip => ip, :active => 1, :since => Time.now, :refreshed => Time.now).save Mac.new(:mac => mac, :ip => ip, :active => 1, :since => Time.now, :refreshed => Time.now).save
Rails.logger.info MacLog.new(:mac => mac, :ip => ip, :action => "activate").save Rails.logger.info MacLog.new(:mac => mac, :ip => ip, :action => "activate").save
} }
@log = MacLog.all @log = MacLog.all