Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save aont/3acb8e81c20912d5b95a to your computer and use it in GitHub Desktop.
Get MacAddress - IPAddress table for PR-400NE
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'nokogiri'
require 'net/http'
require 'net/https'
hostname_tbl = {}
hostname_tbl["01:23:45:67:89:Ab"]="hoge.local"
def xpath_class(classname)
return "contains(concat(\" \",normalize-space(@class),\" \"), \" #{classname} \")"
end
req = Net::HTTP::Get.new("/index.cgi/info_dhcpv4server")
req.basic_auth "user", "pass"
http = Net::HTTP.start("192.168.1.1")
res = http.request(req)
if res.code == "200"
html = Nokogiri::HTML(res.body)
http.finish
# trs = html.xpath("//table[.//label[text()=\"アサインテーブル\"]]//tr[position()>2]")
trs = html.xpath("//table[.//label[text()=\"アサインテーブル\"]]//tr[./td[#{xpath_class("matrix_item")}]]")
trs.each do |tr|
# puts "%s\t%s" % [tr.xpath("./td[1]").text, tr.xpath("./td[2]").text]
ipaddress_td = tr.xpath("./td[1]").text
ipaddress_md = ipaddress_td.match(/(\d+[.]\d+[.]\d+[.]\d+)/)
ipaddress = ipaddress_md[1]
macaddress_td = tr.xpath("./td[2]").text
macaddress_md = macaddress_td.match(/(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/)
macaddress = macaddress_md[1]
# printf "%s %s\n", ipaddress_md[1], macaddress_md[1]
if hn = hostname_tbl[macaddress]
printf "%s %s\n", ipaddress, hn
end
end
else
http.finish
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment