Browse files

* soporte de zonas inversa

* version 0.4
  • Loading branch information...
1 parent 706d8bb commit ed8c3384b2fd155b50af9902ca1089e83ab9699e Sergio Rubio committed Dec 10, 2008
Showing with 96 additions and 32 deletions.
  1. +8 −8 bin/knocked
  2. +3 −1 knocked.gemspec
  3. +85 −23 lib/knocked.rb
View
16 bin/knocked
@@ -6,12 +6,12 @@ rescue Exception
end
def app
- @app ||= NocDns::WebApp.login
+ @app ||= Knocked::WebApp.login
end
cmd = CmdParse::CommandParser.new( true, true )
cmd.program_name = "knocked"
-cmd.program_version = [0, 3, 1]
+cmd.program_version = Knocked::VERSION.split('.')
cmd.options = CmdParse::OptionParserWrapper.new do |opt|
opt.separator "Global options:"
opt.on("--verbose", "Be verbose when outputting info") {|t| $verbose = true }
@@ -27,8 +27,8 @@ list_all = CmdParse::Command.new( 'all', false )
list_all.short_desc = "list all domains from every managed zone"
list_all.set_execution_block do |args|
begin
- puts app.list_domains
- rescue NocDns::TooManyValuesException
+ puts app.list_records
+ rescue Knocked::TooManyValuesException
puts "More than 10 zones available (#{app.available_zones.size})."
puts "Restrict your query to avoid excessive server load."
end
@@ -44,8 +44,8 @@ list_zone.set_execution_block do |args|
$stderr.puts "Skipping invalid zone #{z}"
else
begin
- puts app.list_domains(z)
- rescue NocDns::InvalidZoneException
+ puts app.list_records(z)
+ rescue Knocked::InvalidZoneException
puts "Zone #{z} not under your control. Can't be listed."
end
end
@@ -66,7 +66,7 @@ find_all.set_execution_block do |args|
puts "#{findings.size} matches found"
puts
puts findings
- rescue NocDns::TooManyValuesException
+ rescue Knocked::TooManyValuesException
puts "More than 10 zones available (#{app.available_zones.size})."
puts "Restrict your query to avoid excessive server load."
end
@@ -78,7 +78,7 @@ find_inzone.short_desc = "find all domains matching expression from specified zo
find_inzone.set_execution_block do |args|
begin
findings = app.find(args[1], args[0])
- rescue NocDns::TooManyValuesException
+ rescue Knocked::TooManyValuesException
puts "More than 10 zones available (#{app.available_zones.size})."
puts "Restrict your query to avoid excessive server load."
end
View
4 knocked.gemspec
@@ -1,6 +1,8 @@
+require 'lib/knocked.rb'
+
Gem::Specification.new do |s|
s.name = %q{knocked}
- s.version = "0.3.1"
+ s.version = Knocked::VERSION
s.specification_version = 2 if s.respond_to? :specification_version=
View
108 lib/knocked.rb
@@ -18,11 +18,36 @@
exit 1
end
-module NocDns
+module Knocked
+
+ VERSION = '0.4'
class InvalidSettingsException < Exception; end
class InvalidZoneException < Exception; end
class TooManyValuesException < Exception; end
+ class PtrRecord
+ attr_accessor :id, :name, :ip, :aliases
+
+ def to_s
+ "#{@id}: #{@ip} #{@name}"
+ end
+
+ def =~(r)
+ @name =~ r or @ip =~ r
+ end
+ end
+
+ class ARecord
+ attr_accessor :id, :name, :ip
+ def to_s
+ "#{@id}: #{@ip} #{@name}"
+ end
+
+ def =~(r)
+ @name =~ r or @ip =~ r
+ end
+ end
+
class WebApp
attr_reader :agent
@@ -100,48 +125,85 @@ def available_zones
@zones
end
- def list_domains(zone=nil)
- domains = []
- if zone.nil? or zone.strip.chomp.empty?
- zones = available_zones.keys
- else
- zones = [zone]
- end
+ def list_records(zone=nil)
+ records = []
+ zones = [].concat([zone] || available_zones.keys)
raise TooManyValuesException.new('Too many zones found. Limit your query.') if zones.size > 10
zones.each do |z|
- domains.concat list_zone_domains(z)
+ if z =~ /^\d{1,3}[\.0-9]*/
+ records.concat list_zone_ptrs(z)
+ else
+ records.concat list_zone_domains(z)
+ end
end
- return domains
+ return records
end
def find(exp, zone=nil)
+ exp = '.*' if exp.nil? or exp.empty?
findings = []
- list_domains(zone).each do |d|
- findings << d if d =~ /#{exp}/
+ zones = [].concat([zone] || available_zones.keys)
+ zones.each do |z|
+ list_records(z).each do |d|
+ findings << d if d =~ /#{Regexp.quote(exp)}/
+ end
end
return findings
end
def barbarize(interval=1)
- domains = []
+ records = []
zones = available_zones.keys
zones.each do |z|
yield z if block_given?
- domains.concat list_zone_domains(z)
+ records.concat list_records(z)
sleep(interval)
end
- return domains
+ return records
end
private
+ def list_zone_ptrs(zone)
+ if not available_zones.has_key? zone
+ raise InvalidZoneException.new("zone mapping not found for #{zone}")
+ end
+ ptrs = []
+ params = {}
+ id = available_zones[zone]
+ pdata = "pagina=Ptr&iddnssoa=#{id}&dominio=#{zone}&ip=si&tipoAccion=view"
+ pdata.split('&').each do |p|
+ key, val = p.split('=')
+ params[key] = val
+ end
+ response = @agent.get("#{settings['app_url']}/GestionNavegacionDns", params)
+ raise Exception.new('Invalid response from web server.') if response.code != '200'
+ doc = Hpricot(response.parser.to_s)
+ doc.search("//tr[@class='registros1']").each do |reg|
+ fields = (reg/'td')
+ ptr = PtrRecord.new
+ ptr.id = fields[0].inner_text
+ ptr.ip = fields[1].inner_text
+ ptr.name = fields[2].inner_text.gsub("?", "")
+ # aliases reverse zones?
+ # we don't need them right now
+ #ptr.aliases = fields[3].to_s.split(/<br\s?\/>/).collect do |i|
+ # val = i.gsub(/<br\s?\/?>|&nbsp;|<\/?td>/, '').strip
+ # val = nil if val.empty?
+ # val
+ #end
+ ptrs << ptr
+ end
+ return ptrs
+ end
+
def list_zone_domains(zone)
if not available_zones.has_key? zone
raise InvalidZoneException.new("zone mapping not found for #{zone}")
end
domains = []
params = {}
id = available_zones[zone]
- pdata = "pagina=A&iddnssoa=#{id}&dominio=#{zone}&ip=no&tipoAccion=adm"
+ pdata = "pagina=A&iddnssoa=#{id}&dominio=#{zone}&ip=no&tipoAccion=view"
pdata.split('&').each do |p|
key, val = p.split('=')
params[key] = val
@@ -150,13 +212,13 @@ def list_zone_domains(zone)
response = @agent.get("#{settings['app_url']}/GestionNavegacionDns", params)
raise Exception.new('Invalid response from web server.') if response.code != '200'
doc = Hpricot(response.parser.to_s)
- doc.search("//tr[@class='registros2']").each do |reg|
- text = (reg/'td').inner_text.chomp.strip
- if not text.empty?
- d,i = text.split[1..-1]
- fqdn = "#{d}.#{zone}".ljust(50)
- domains << "#{fqdn} #{i}"
- end
+ doc.search("//tr[@class='registros1']").each do |reg|
+ fields = (reg/'td')
+ a = ARecord.new
+ a.id = fields[0].inner_text.strip.chomp
+ a.name = fields[1].inner_text.gsub("?", "").strip.chomp + ".#{zone}"
+ a.ip = fields[2].inner_text.strip.chomp
+ domains << a
end
return domains
end

0 comments on commit ed8c338

Please sign in to comment.