Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple Ruby script to retrieve some useful Internet connexion statistics from a Siemens Gigaset sx762 modem/router. By default it only returns the Up- and Down-stream statistics, External IP & MAC addresses and Uptime, but if you pass the --verbose argument, you get the raw data back as well.
#!/usr/bin/env ruby
# router_statistics.rb: Simple Ruby script to retrieve some useful Internet connexion statistics from a Siemens Gigaset sx762 modem/router
# (c) 2013, Filip H.F. "FiXato" Slagter <fixato+router_statistics@gmail.com>
# By default it only returns the Up- and Down-stream statistics, External IP & MAC addresses and Uptime, but if you pass the --verbose argument, you get the raw data back as well.
#
# Sample output (with faked IP/MAC addresses):
# Siemens Gigaset sx762 Statistics:
# ================================================================================
# Up: 833 / 825 kbps [Attn: 408 dB / Noise: 56 dB / Pwr: 122 dBm]
# Down: 1309 / 1316 kpbs [Attn: 602 dB / Noise: 61 dB / Pwr: 149 dBm]
# IP/MAC: 203.0.113.0 / 10:00:00:00:FF
# Uptime: 1061 minutes (~= 17.69 hours) (~= 0.74 days)
ROUTER_PASSWORD='ReplaceWithYourRouterPassword'
ROUTER_URL = 'http://192.168.2.1'
cmd=<<-EOS
curl --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1626.0 Safari/537.36" \
--silent \
-L \
-e #{ROUTER_URL}/UE/welcome_login.html \
--data "form_submission_type=login" \
--data "form_submission_parameter=" \
--data "current_page=welcome_login.html" \
--data "next_page=status_internet.html" \
--data "i=1" \
--data "your_password=#{ROUTER_PASSWORD}" \
--data "Login=OK" \
"#{ROUTER_URL}/UE/ProcessForm" \
| grep scmData
EOS
stats = {}
json_string = `#{cmd}`.gsub('var scmData = ','').strip[0...-1]
# Parse the flattened key/value pairs from the json_string into a simple hash
json_string.scan(/\["([^\[]+)", "([^\[]+)"\]/) do |m|
stats[m[0]] = m[1]
end
puts 'Siemens Gigaset sx762 Statistics:'
puts '='*80
puts ' Up: %5i / %5i kbps [Attn: %3i dB / Noise: %2i dB / Pwr: %3i dBm]' % [stats['UpstreamCurrRate'], stats['UpstreamMaxRate'], stats['UpstreamAttenuation'], stats['UpstreamNoiseMargin'], stats['UpstreamPower']]
puts ' Down: %5i / %5i kpbs [Attn: %3i dB / Noise: %2i dB / Pwr: %3i dBm]' % [stats['DownstreamCurrRate'], stats['DownstreamMaxRate'], stats['DownstreamAttenuation'], stats['DownstreamNoiseMargin'], stats['DownstreamPower']]
puts 'IP/MAC: %s / %s' % [stats['ExternalIPAddress'], stats['V_MACAddress']]
puts 'Uptime: %i minutes (~= %0.2f hours) (~= %0.2f days)' % [stats['Uptime'].to_i / 60.00, stats['Uptime'].to_i / 3600.00, stats['Uptime'].to_i / 86400.00]
puts
if ARGV.include?('--verbose')
require 'yaml'
require 'json'
puts "JSON string: ", json_string
puts ""
puts "Parsed JSON in YAML format:", JSON.parse(json_string).to_yaml
puts ""
puts "Parsed stats: ", stats.to_yaml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment