Skip to content

Instantly share code, notes, and snippets.

@pennig
Created February 11, 2012 06:25
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 pennig/1797222 to your computer and use it in GitHub Desktop.
Save pennig/1797222 to your computer and use it in GitHub Desktop.
Read my cable modem's status page, and store as JSON into a txt file
require 'nokogiri'
require 'json'
require 'time'
require 'timeout'
def parse
doc = Nokogiri::HTML(`curl -s http://admin:password@192.168.100.1/cgi-bin/ConnectionCgi`)
error_log = Nokogiri::HTML(`curl -s http://admin:password@192.168.100.1/cgi-bin/EventLogCgi`)
tables = doc.css('td table')
errors = error_log.css('td table tr').size - 1
date = doc.css('font')
data = {
'T' => Time.parse(date.children[1].text).to_i,
'e' => errors,
'd' => [],
'u' => []
}
tables[1..4].each do |table|
tds = table.css('td')
data['d'] << {
'p' => tds[12].text.split(' ').first,
'r' => tds[14].text.split(' ').first,
'c' => tds[16].text,
'u' => tds[18].text
}
end
tables[5..8].each do |table|
tds = table.css('td')
data['u'] << {
'p' => tds[12].text.split(' ').first
}
end
data.to_json
end
#
loop do
out = ''
begin
Timeout.timeout(2) { out = parse() }
rescue
out = {
'E' => "timeout",
'T' => Time.new.to_i
}.to_json
end
file = Time.now.to_date.strftime('%Y%m%d')
File.open("/Users/matt/cable_log/#{file}.txt", 'a') { |f| f.puts(out) }
puts "Wrote #{out.size} bytes"
sleep(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment