Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created April 8, 2011 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchrisadams/910434 to your computer and use it in GitHub Desktop.
Save mrchrisadams/910434 to your computer and use it in GitHub Desktop.
require File.join File.expand_path(File.dirname(__FILE__)), 'environment.rb'
# this should really a separate class test, I know...
# testing the output
class NetworkScannerTest < Test::Unit::TestCase
include FlexMock::TestCase
def setup
@network_scanner = NetworkScanner.new
end
context "the network scanner" do
test "checks if nmap is installed" do
assert @network_scanner.is_nmap_present?, true
end
test "checks is arp is available" do
assert @network_scanner.is_arp_present?, true
end
test "works out it'sj own IP addess to use when network sweeping" do
assert @network_scanner.local_ip, true
assert @network_scanner.local_ip.match('([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3})')
end
context "sweeps the network and " do
setup do
@mocked_network_scanner = flexmock(@network_scanner, :mac_sweep => true)
@local_ip_address = @network_scanner.local_ip
@active_macs = @network_scanner.mac_sweep(@local_ip_address)
@nmap_output = File.readlines('tests/nmap.greppable.output.txt')
end
# not sure how to do this one tbh...
# how am I supposed to test for this, without is being a useless
test "pipes the greppable input into a temporary file" do
pending
end
test "reads the temp file to pull out a collection of ip addresses to lookup" do
assert_equal "192.168.3.1", @network_scanner.parse_ip(@nmap_output[1])
assert_equal "192.168.3.9", @network_scanner.parse_ip(@nmap_output[2])
# and so on for all IP's
end
test "reads the temp file to pull out a collection of device names" do
assert_equal "unknown", @network_scanner.parse_name(@nmap_output[1])
assert_equal "", @network_scanner.parse_name(@nmap_output[2])
assert_equal "mark-laptop", @network_scanner.parse_name(@nmap_output[7])
# and so on for all IP's
end
test "reads the temp file to pull out device names if present" do
pending do
discovered_devices = @network_scanner.build_device_list
assert_equal 39, discovered_devices.length
end
end
test "calls arp and parses out active macs and ip addresses" do
pending
end
end
end
end
class MacCheckerTest < Test::Unit::TestCase
def setup
end
context "checking a group of scanned macs with one known user" do
test "finds a valid user" do
pending
end
test "looks for a device with matching mac address in the db" do
pending
end
test "creates a trace to represent the person in that space" do
pending
end
context "a single user is found" do
test "a trace of a person expires after a given time" do
pending
end
test "a single user is linked to a device with a mac address" do
end
end
end
end
class NetworkScanner
require 'socket'
# convenience method to get the IP address, assuming the 'pooter
# running it only has a single network interface
# @return String ipadress
#
# taken from http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
# set up a call to be ready to connect to Google' free DNS server
# wokring out how to send packets along the way. This gives us the
# local IP address to end packets from
UDPSocket.open do |s|
s.connect '8.8.8.8', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
# TODO use popen4 to only return the true/false status, instead of
# dropping command output into SDOUT.
def is_arp_present?
true if system "which arp"
end
# simple check to see if nmap exists on the machine
# TODO give instructions on how to install it, depending on platform
def is_nmap_present?
true if system "which nmap"
end
# Call nmap to refresh ARP cache, and output a greppable list
def mac_sweep(ip_address)
system "nmap -sP -R -oG nmap.output #{ip_address}/24"
end
# Parse a single nmap string, and pull out the IP address
# @params String nmap_line
# #returns String ip_address
def parse_ip nmap_line
ip_regex = '[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}'
nmap_line.match(ip_regex).to_s
end
# Parse the device name if it exists from an nmap line
def parse_name nmap_line
name_regex = '\(([a-zA-Z0-9-_]+)\)'
nmap_line.match(name_regex).to_s
end
# parse the lin
def build_device_list nmap_line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment