Skip to content

Instantly share code, notes, and snippets.

@libo
Last active October 18, 2015 15:10
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 libo/5c251573caa353eec0da to your computer and use it in GitHub Desktop.
Save libo/5c251573caa353eec0da to your computer and use it in GitHub Desktop.
Script to fetch Signa statistic from a HUAWEI CPE via console.
#!/usr/bin/env ruby
# I wrote this script to fetch signal information every 5 minutes and plot a graph based on RSRQ RSRP using Munin / MRTG
# HUAWEI if you don't like what I did please let me know I will remove this script pronto.
#
# Update BASE_URL with the correct IP of your CPE.
# Usage:
# ruby cpe_signal.rb
#
# will print something like this
# <?xml version="1.0" encoding="UTF-8"?>
# <response>
# <pci>22</pci>
# <sc></sc>
# <cell_id>12345</cell_id>
# <rsrq>-7dB</rsrq>
# <rsrp>-76dbm</rsrp>
# <rssi>&gt;=-51dBm</rssi>
# <sinr></sinr>
# <rscp></rscp>
# <ecio></ecio>
# <mode>7</mode>
# </response>
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
BASE_URL = 'http://192.168.8.1'
def session_info
uri = URI.parse(BASE_URL+'/html/index.html')
@csrf_token ||= begin
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
csrf_token_regexp = /name=\"csrf_token\" content=\"(.*)\"/
{
csrf_token: response.body.match(csrf_token_regexp)[1],
cookie: response['set-cookie'].split(/\;/)[0]
}
end
end
def signal_stats
uri = URI.parse("#{BASE_URL}/api/device/signal")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request["Cookie"] = session_info[:cookie]
request["__RequestVerificationToken"] = session_info[:csrf_token]
response = http.request(request)
response.body
end
puts signal_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment