Created
June 13, 2011 03:59
-
-
Save henare/1022296 to your computer and use it in GitHub Desktop.
Scrape your Linksys AM300 to get line stats (should be available via SNMP!). Now available as a repo https://github.com/henare/linksys_am300_statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './dslmodem.rb' | |
raise "Incorrect number of arguments supplied. Need URL, username and password" if ARGV.size != 3 | |
modem = DSLModem.new(ARGV[0], ARGV[1], ARGV[2]) | |
while true | |
modem.get_stats_page | |
statistics = modem.statistics | |
status = if modem.offline? | |
"Offline" | |
else | |
"Online. Bandwidth: #{statistics[:bandwidth_downstream]}/#{statistics[:bandwidth_upstream]}. Margin: #{statistics[:margin_downstream]}/#{statistics[:margin_upstream]}" | |
end | |
puts "#{Time.now}: #{status}" | |
sleep 60 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
class DSLModem | |
def initialize(url='http://192.168.1.1/index.stm?title=Status-Modem', username, password) | |
@url, @username, @password = url, username, password | |
get_stats_page | |
end | |
def get_stats_page | |
# Get the login page | |
agent = Mechanize.new | |
@page = agent.get(@url) | |
# If we're already logged in (don't ask), then we already have the page we need | |
return @page if @page.title != "Log In" | |
# Login | |
form = @page.forms.first | |
form.field_with(:name => "username").value = @username | |
form.field_with(:name => "password").value = @password | |
form.submit | |
# Get stats page | |
@page = agent.get(@url) | |
end | |
def statistics | |
status_table = @page.search("#table1")[1] | |
{ | |
:bandwidth_downstream => @page.search("#table1")[1].search('b')[3].inner_text.strip.split[0], | |
:bandwidth_upstream => @page.search("#table1")[1].search('b')[4].inner_text.strip.split[0], | |
:margin_downstream => @page.search("#table1")[1].search('b')[5].inner_text.strip.split[0], | |
:margin_upstream => @page.search("#table1")[1].search('b')[6].inner_text.strip.split[0] | |
} | |
end | |
def offline? | |
@page.search("#table1")[1].search('b')[3].inner_text.strip.split[0] == "0" ? true : false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment