Permalink
Browse files

Attach snitch to renee

Now serves usernames up, which are configurable
Example config file added
  • Loading branch information...
1 parent c720824 commit a1f96849221697b9401632439ee48e80358ba722 @martinisoft martinisoft committed Jan 6, 2012
Showing with 69 additions and 27 deletions.
  1. +1 −0 .gitignore
  2. +17 −0 config.ru
  3. +1 −0 snitch.example.yml
  4. +50 −0 snitch.rb
  5. +0 −27 snmp.rb
View
1 .gitignore
@@ -0,0 +1 @@
+snitch.yml
View
17 config.ru
@@ -0,0 +1,17 @@
+$: << File.dirname(__FILE__)
+
+require 'snitch'
+require 'renee'
+
+snitch = Snitch.new
+
+run Renee.core {
+ @snitch = snitch
+
+ path('who') do
+ get do
+ @snitch.update_client_list
+ halt @snitch.connected_clients.join(",")
+ end
+ end
+}
View
1 snitch.example.yml
@@ -0,0 +1 @@
+router_name: apextreme.local
View
50 snitch.rb
@@ -0,0 +1,50 @@
+require 'snmp'
+require 'yaml'
+
+class Snitch
+ attr_accessor :client_list, :config
+
+ def initialize
+ @client_list = []
+ @config = YAML.load_file("snitch.yml")
+ end
+
+ def update_client_list
+ client_list.clear
+ # Open SNMP connection
+ SNMP::Manager.open(host: config['router_name']) do |manager|
+ # Walk the SNMP Table for Net Addresses, see RFC 1213-MIB
+ manager.walk("ipNetToMediaPhysAddress") do |row|
+ # Re-encode and process the OCTET String that comes back
+ addr = process_client_address(row.value.encode.to_s)
+ # Squish them together to form a MAC address
+ client_list << addr.join(":").upcase
+ end
+ end
+ end
+
+ def connected_clients
+ client_list.map do |client|
+ config[client]
+ end.reject { |c| c.nil? }
+ end
+
+ private
+
+ def process_client_address(address)
+ mac = []
+ # Loop over each byte to a nicer HEX value
+ address.each_byte do |c|
+ if c.to_s(16) == "0"
+ mac << "00"
+ elsif c.to_s(16).size == 1
+ mac << "0#{c.to_s(16)}"
+ else
+ mac << c.to_s(16)
+ end
+ end
+ # The table returns 2 extra numbers in front, drop 'em
+ mac.shift(2)
+ mac
+ end
+end
View
27 snmp.rb
@@ -1,27 +0,0 @@
-require 'snmp'
-
-ROUTER_NAME='andromeda.local'
-
-# Open SNMP connection
-SNMP::Manager.open(host: ROUTER_NAME) do |manager|
- # Walk the SNMP Table for Net Addresses, see RFC 1213-MIB
- manager.walk("ipNetToMediaPhysAddress") do |row|
- # Re-encode the OCTET String that comes back
- addr = row.value.encode.to_s
- mac = []
- # Loop over each byte to a nicer HEX value
- addr.each_byte do |c|
- if c.to_s(16) == "0"
- mac << "00"
- elsif c.to_s(16).size == 1
- mac << "0#{c.to_s(16)}"
- else
- mac << c.to_s(16)
- end
- end
- # The table returns 2 extra numbers in front, drop 'em
- mac.shift(2)
- # Squish them together to form a MAC address
- pp mac.join(":")
- end
-end

0 comments on commit a1f9684

Please sign in to comment.