Skip to content

Instantly share code, notes, and snippets.

@thepatrick
Created October 24, 2010 11:40
Show Gist options
  • Save thepatrick/643468 to your computer and use it in GitHub Desktop.
Save thepatrick/643468 to your computer and use it in GitHub Desktop.
Netcomm NB6Plus4 Stats Extractor
# NB6Plus4 Stats Extractor
# Copyright (c) 2010 Patrick Quinn-Graham
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'net/telnet'
require 'pp'
def first_match(thing)
thing && thing[1]
end
def sanity(thing)
thing && [thing[1], thing[2]]
end
def calc_time(from_string)
days, hours, minutes, seconds = from_string.split(":")
seconds.to_i + (minutes.to_i * 60) + (hours.to_i * 60 * 60) + (days.to_i * 60 * 60 * 24)
end
if ARGV.length < 3
puts "nb6plus4.rb: NB6Plus4 Stats Extractor"
puts "Usage: ruby nb6plus4.rb router-ip username password [format]"
puts "Format: 'csv' for a comma delimited output (in order shown in normal mode), leave off for a human friendly version."
exit
end
tn = Net::Telnet::new("Host" => ARGV[0], "Timeout" => 10, "Prompt" => /^\>/)
tn.login("Name" => ARGV[1], "Password" => ARGV[2], "LoginPrompt" => /^Login name:/, "PasswordPrompt" => /^Password\:/)
adslstatus = ""
adslinfo = ""
adslstats = ""
tn.cmd("adsl adslstatus") { |rcvdata| adslstatus += rcvdata }
tn.cmd("adsl info") { |rcvdata| adslinfo += rcvdata }
tn.cmd("adsl info --stats") { |rcvdata| adslstats += rcvdata }
link_state = first_match /Link State[\s]+\: ([^\n]+)/.match(adslstatus)
dsl_up_time = first_match /DSL Line Up Time[\s]+\: ([^\n]+)/.match(adslstatus)
sys_up_time = first_match /System Up Time[\s]+\: ([^\n]+)/.match(adslstatus)
upstream = first_match /Upstream rate = ([0-9]+)/.match(adslinfo)
downstream = first_match /Downstream rate = ([0-9]+)/.match(adslinfo)
snr_down, snr_up = sanity /SNR \(dB\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
attn_down, attn_up = sanity /Attn\(dB\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
pwr_down, pwr_up = sanity /Pwr\(dBm\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
max_down, max_up = sanity /Max\(Kbps\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
if ARGV[3] and ARGV[3] == "csv"
puts [link_state == "Show Time", calc_time(dsl_up_time), calc_time(sys_up_time), upstream, downstream, snr_down, snr_up, attn_down, attn_up, pwr_down, pwr_up, max_down, max_up].join(",")
else
puts "Link State: " + (link_state == "Show Time" ? "Up" : "Down")
puts "DSL Uptime: " + calc_time(dsl_up_time).to_s + " seconds"
puts "System Uptime: " + calc_time(sys_up_time).to_s + " seconds"
puts "Upstream: " + upstream + " Kbps"
puts "Downstream: " + downstream + " Kbps"
puts "SNR Down: " + snr_down + " dB"
puts "SNR Up: " + snr_up + " dB"
puts "Attn Down: " + attn_down + " dB"
puts "Attn Up: " + attn_up + " dB"
puts "Pwr Down: " + pwr_down + " dBm"
puts "Pwr Up: " + pwr_up + " dBm"
puts "Max Down: " + max_down + " Kbps"
puts "Max Up: " + max_up + " Kbps"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment