Skip to content

Instantly share code, notes, and snippets.

@spig
Last active April 5, 2021 19:37
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 spig/0e8713947f7eb5d95f7761fb1f6bdddf to your computer and use it in GitHub Desktop.
Save spig/0e8713947f7eb5d95f7761fb1f6bdddf to your computer and use it in GitHub Desktop.
Get IP Address for ddclient from a TP-LINK Router

Get IP Address for use in ddclient from a TP-LINK Router

Retrieve the local IP address by screen scraping the router's web page using a ruby script from a server behind the router

To use in ddclient:

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment