Skip to content

Instantly share code, notes, and snippets.

@aont
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save aont/9445861 to your computer and use it in GitHub Desktop.

Select an option

Save aont/9445861 to your computer and use it in GitHub Desktop.
PR-400NE,KIでWANアドレス監視
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'nokogiri'
require 'net/http'
require 'net/https'
$router_user = "hoge"
$router_pass = "fuga"
$noip_user = "hoge"
$noip_pass = "fuga"
$noip_hostname = "hoge"
$ddo_dn = "hoge.ddo.jp"
$ddo_pw = "hoge"
def fputs_mes(fp, mes, string)
string.each_line do |line|
fp.puts "#{mes} #{line}"
end
end
def get_ipaddress()
req = Net::HTTP::Get.new("/index.cgi/info2_main")
req.basic_auth $router_user, $router_pass
begin
http = Net::HTTP.start("192.168.1.1")
rescue => ex
fputs_mes($stderr,"[sys]", ex.message)
return ""
end
begin
res = http.request(req)
rescue => ex
fputs_mes($stderr,"[sys]", ex.message)
return ""
end
if res.code == "200"
html = Nokogiri::HTML(res.body)
http.finish
# html = Nokogiri::HTML(open('http://192.168.1.1/cgi-bin/paractl.cgi?st_state', :http_basic_authentication => [$router_user, $router_pass]))
## return html.xpath("//td[@id='wanIP0']").text()
html.xpath("//tr[td/label[text()='WAN側IPアドレス']]/td[position()=2]")[0].text
else
http.finish
fputs_mes($stderr,"[sys]", res.body)
return ""
end
end
def update_noip(ipaddress)
req = Net::HTTP::Get.new("/nic/update?hostname=#{$noip_hostname}")
req.basic_auth $noip_user, $noip_pass
http = Net::HTTP.new("dynupdate.no-ip.com","443")
http.use_ssl = true
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
begin
http.start
rescue => ex
return ex.message
end
begin
res = http.request(req)
rescue => ex
return ex.message
end
http.finish
return res.body
# if res.code == "200"
# return res.body
# else
# return res.body
# #return "update_noip failed! (HTTP code != 200)"
# end
end
def update_ddo(ipaddress)
req = Net::HTTP::Get.new("/dnsupdate.php?dn=#{$ddo_dn}&pw=#{$ddo_pw}")
begin
http = Net::HTTP.start("free.ddo.jp")
rescue => ex
return ex.message
end
begin
res = http.request(req)
rescue => ex
return ex.message
end
http.finish
return res.body.encode("UTF-8", "EUCJP")
# if res.code != "200"
# return res.body.encode("UTF-8", "EUCJP")
# end
# return res.body.encode("UTF-8", "EUCJP")
end
ipaddress = ""
while true
ipaddress_new = get_ipaddress
if ipaddress_new.empty?
sleep 1
next
end
t = Time::now
ti = t.to_i
if ipaddress_new != ipaddress
ipaddress = ipaddress_new
$stdout.puts "%s %s chg" % [ti, ipaddress]
fputs_mes($stderr,"[noip]", update_noip(ipaddress))
fputs_mes($stderr,"[ddo]", update_ddo(ipaddress))
f = open("ip.txt", "a")
f.puts "%s %s %s" % [ti, t.strftime("%m/%d-%H:%M"), ipaddress]
f.close
system("dropbox_uploader upload ./ip.txt / 1>dropbox.out 2>dropbox.err")
else
$stdout.puts "%s %s nochg" % [ti, ipaddress]
end
$stdout.flush
$stderr.flush
sleep 10
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment