Guest User

Untitled

a guest
Jun 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. # NB6Plus4 Stats Extractor
  2. # Copyright (c) 2010 Patrick Quinn-Graham
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. require 'net/telnet'
  24. require 'pp'
  25.  
  26. def first_match(thing)
  27. thing && thing[1]
  28. end
  29. def sanity(thing)
  30. thing && [thing[1], thing[2]]
  31. end
  32. def calc_time(from_string)
  33. days, hours, minutes, seconds = from_string.split(":")
  34. seconds.to_i + (minutes.to_i * 60) + (hours.to_i * 60 * 60) + (days.to_i * 60 * 60 * 24)
  35. end
  36.  
  37. if ARGV.length < 3
  38. puts "nb6plus4.rb: NB6Plus4 Stats Extractor"
  39. puts "Usage: ruby nb6plus4.rb router-ip username password [format]"
  40. puts "Format: 'csv' for a comma delimited output (in order shown in normal mode), leave off for a human friendly version."
  41. exit
  42. end
  43.  
  44. tn = Net::Telnet::new("Host" => ARGV[0], "Timeout" => 10, "Prompt" => /^\>/)
  45.  
  46. tn.login("Name" => ARGV[1], "Password" => ARGV[2], "LoginPrompt" => /^Login name:/, "PasswordPrompt" => /^Password\:/)
  47.  
  48. adslstatus = ""
  49. adslinfo = ""
  50. adslstats = ""
  51.  
  52. tn.cmd("adsl adslstatus") { |rcvdata| adslstatus += rcvdata }
  53. tn.cmd("adsl info") { |rcvdata| adslinfo += rcvdata }
  54. tn.cmd("adsl info --stats") { |rcvdata| adslstats += rcvdata }
  55.  
  56. link_state = first_match /Link State[\s]+\: ([^\n]+)/.match(adslstatus)
  57. dsl_up_time = first_match /DSL Line Up Time[\s]+\: ([^\n]+)/.match(adslstatus)
  58. sys_up_time = first_match /System Up Time[\s]+\: ([^\n]+)/.match(adslstatus)
  59. upstream = first_match /Upstream rate = ([0-9]+)/.match(adslinfo)
  60. downstream = first_match /Downstream rate = ([0-9]+)/.match(adslinfo)
  61.  
  62. snr_down, snr_up = sanity /SNR \(dB\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
  63. attn_down, attn_up = sanity /Attn\(dB\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
  64. pwr_down, pwr_up = sanity /Pwr\(dBm\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
  65. max_down, max_up = sanity /Max\(Kbps\):[\s]+([0-9\.]+)[\s]+([0-9\.]+)/.match(adslstats)
  66.  
  67. if ARGV[3] and ARGV[3] == "csv"
  68. 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(",")
  69. else
  70. puts "Link State: " + (link_state == "Show Time" ? "Up" : "Down")
  71. puts "DSL Uptime: " + calc_time(dsl_up_time).to_s + " seconds"
  72. puts "System Uptime: " + calc_time(sys_up_time).to_s + " seconds"
  73. puts "Upstream: " + upstream + " Kbps"
  74. puts "Downstream: " + downstream + " Kbps"
  75. puts "SNR Down: " + snr_down + " dB"
  76. puts "SNR Up: " + snr_up + " dB"
  77. puts "Attn Down: " + attn_down + " dB"
  78. puts "Attn Up: " + attn_up + " dB"
  79. puts "Pwr Down: " + pwr_down + " dBm"
  80. puts "Pwr Up: " + pwr_up + " dBm"
  81. puts "Max Down: " + max_down + " Kbps"
  82. puts "Max Up: " + max_up + " Kbps"
  83. end
Add Comment
Please, Sign In to add comment