Retrieve the local IP address by screen scraping the router's web page using a ruby script from a server behind the router
use=cmd, cmd="ROUTER_IP=<YOUR ROUTER IP ADDRESS> ROUTER_USER=<USERNAME> ROUTER_PASS=<PASSWORD> /path/to/getRouterIPAddress.rb"
| #!/usr/bin/ruby | |
| require 'base64' | |
| require 'uri' | |
| require 'net/http' | |
| ROUTER_IP = ENV['ROUTER_IP'] | |
| ROUTER_USER = ENV['ROUTER_USER'] | |
| ROUTER_PASS = ENV['ROUTER_PASS'] | |
| # get ip from router | |
| def routerIP | |
| uri = URI("http://"+ROUTER_IP+"/data/map_internet_info_form.json") | |
| req = Net::HTTP::Post.new(uri) | |
| req['Referer'] = "http://"+ROUTER_IP+"/userRpm/BasicNetworkMapRpm.htm" | |
| req['Cookie'] = URI::encode("Authorization=Basic "+Base64.strict_encode64(ROUTER_USER+':'+ROUTER_PASS)) | |
| req.set_form_data('operation' => 'read') | |
| res = Net::HTTP.start(uri.hostname, uri.port) do |http| | |
| http.request(req) | |
| end | |
| case res | |
| when Net::HTTPSuccess, Net::HTTPRedirection | |
| puts res.body | |
| res.body.each_line do |line| | |
| # "wan_ip": "0.0.0.0" | |
| line.match /wan_ip".*"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"/ do |matchdata| | |
| return matchdata[1] | |
| end | |
| end | |
| else | |
| puts res.value | |
| end | |
| return nil | |
| end | |
| puts routerIP() |