基于属性值的 Nokogiri 条件
Nokogiri condition based on attribute value
我正在尝试使用基于现有属性值的 Nokogiri 潜在地解析两个 XML 文件。问题是我的初始条件总是返回 true,即使该节点不存在于 xpath
。我认为我做对了一切,但显然我没有。
这是我的两个 XML 文件:
dnsrecon_output.xml(典型文件输出)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.1" type="info" zone_transfer="failed"/>
<record ns_server="192.168.1.2" type="info" zone_transfer="failed"/>
<record address="192.168.1.1" mname="ns1.mynetwork.local" type="SOA"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.1" recursive="True" target="ns1.mynetwork.local" type="NS"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.2" recursive="True" target="ns2.mynetwork.local" type="NS"/>
<record address="192.168.1.3" name="web.local" type="A"/>
</records>
dnsrecon_brute.xml
<?xml version="1.0" ?>
<records>
<record address="192.168.1.3" name="dev.web.local" type="A"/>
<record name="ftp.web.local" target="dev.web.local" type="CNAME"/>
<record address="192.168.1.4" name="git.web.local" type="A"/>
<record address="192.168.1.5" name="ops.web.local" type="A"/>
<record address="192.168.1.6" name="whm.web.local" type="A"/>
<record address="192.168.1.7" name="www.web.local" type="A"/>
</records>
但是,如果 zone_transfer=success
输出将如下所示...
dnsrecon_output_xfer.xml(只是为了展示我要匹配的内容)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.2" type="info" zone_transfer="success"/>
<record address="192.168.1.2" mname="ns" type="SOA" zone_server="192.168.1.2"/>
<record address="192.168.1.2" target="ns1.network.local" type="NS" zone_server="192.168.1.2"/>
<record address="192.168.1.100" name="win.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" name="ns1.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" mname="ns1.network.local" type="SOA"/>
</records>
这是我的 Nokogiri 解析。主要是我想检查 zone_transfer=success
是否存在,如果它没有移动到 *_brute.xml
文件:
document = Nokogiri::XML(File.open("dnsrecon_output.xml"))
document.remove_namespaces!
# Strictly for Zone transfers
if document.xpath("//records//record[@zone_transfer='success']")
puts "Zone Transfer Successful ... YIKES!!!"
document.xpath("//records//record[@type='A']").each do |record|
# change the output here to match what we need it to...
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
else
puts "No zone transfers... moving to brute"
document2 = Nokogiri::XML(File.open("dnsrecon_brute.xml"))
document2.remove_namespaces!
document2.xpath("//records//record[@type='A' or @type='CNAME']").each do |record|
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
end
如您所见,我最初解析的是 dnsrecon_output.xml
文件,该文件的记录标记中包含 zone_transfer="failed"
。但是,我在控制台中的输出是:
Zone Transfer Successful ... YIKES!!!
192.168.1.3 - web.local
并且从不运行 else
部分,这是应该的。我在检查属性值时哪里出错了?
您的支票 return 是空的 Nokogiri::XML::NodeSet
,这是真实的。
读取属性并比较其值:
if document.xpath("//records//record/@zone_transfer").first.value == 'success'
...
请注意,这仅在 xpath 至少匹配一个元素时有效(因此它不适用于 "brute" 文件)。
或添加 any?
以获得布尔值 return:
if document.xpath("//records//record[@zone_transfer='success']").any?
...
x = document.xpath("//records//record[@zone_transfer='success']")
p x
--output:--
[]
和:
if []
puts %q{It's true!}
end
--output:--
It's true.
在 ruby 中计算结果为 false 的唯一值是 false
本身和 nil
。空数组、空散列、空字符串、0
等都是 true.
相反,您可以这样做:
if document.xpath("//records//record[@zone_transfer='success']").empty?
puts "no matching elements"
#got to next xml file
else
puts "found matches"
#do your other xpath search
end
--output:--
no matching elements
我正在尝试使用基于现有属性值的 Nokogiri 潜在地解析两个 XML 文件。问题是我的初始条件总是返回 true,即使该节点不存在于 xpath
。我认为我做对了一切,但显然我没有。
这是我的两个 XML 文件:
dnsrecon_output.xml(典型文件输出)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.1" type="info" zone_transfer="failed"/>
<record ns_server="192.168.1.2" type="info" zone_transfer="failed"/>
<record address="192.168.1.1" mname="ns1.mynetwork.local" type="SOA"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.1" recursive="True" target="ns1.mynetwork.local" type="NS"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.2" recursive="True" target="ns2.mynetwork.local" type="NS"/>
<record address="192.168.1.3" name="web.local" type="A"/>
</records>
dnsrecon_brute.xml
<?xml version="1.0" ?>
<records>
<record address="192.168.1.3" name="dev.web.local" type="A"/>
<record name="ftp.web.local" target="dev.web.local" type="CNAME"/>
<record address="192.168.1.4" name="git.web.local" type="A"/>
<record address="192.168.1.5" name="ops.web.local" type="A"/>
<record address="192.168.1.6" name="whm.web.local" type="A"/>
<record address="192.168.1.7" name="www.web.local" type="A"/>
</records>
但是,如果 zone_transfer=success
输出将如下所示...
dnsrecon_output_xfer.xml(只是为了展示我要匹配的内容)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.2" type="info" zone_transfer="success"/>
<record address="192.168.1.2" mname="ns" type="SOA" zone_server="192.168.1.2"/>
<record address="192.168.1.2" target="ns1.network.local" type="NS" zone_server="192.168.1.2"/>
<record address="192.168.1.100" name="win.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" name="ns1.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" mname="ns1.network.local" type="SOA"/>
</records>
这是我的 Nokogiri 解析。主要是我想检查 zone_transfer=success
是否存在,如果它没有移动到 *_brute.xml
文件:
document = Nokogiri::XML(File.open("dnsrecon_output.xml"))
document.remove_namespaces!
# Strictly for Zone transfers
if document.xpath("//records//record[@zone_transfer='success']")
puts "Zone Transfer Successful ... YIKES!!!"
document.xpath("//records//record[@type='A']").each do |record|
# change the output here to match what we need it to...
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
else
puts "No zone transfers... moving to brute"
document2 = Nokogiri::XML(File.open("dnsrecon_brute.xml"))
document2.remove_namespaces!
document2.xpath("//records//record[@type='A' or @type='CNAME']").each do |record|
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
end
如您所见,我最初解析的是 dnsrecon_output.xml
文件,该文件的记录标记中包含 zone_transfer="failed"
。但是,我在控制台中的输出是:
Zone Transfer Successful ... YIKES!!!
192.168.1.3 - web.local
并且从不运行 else
部分,这是应该的。我在检查属性值时哪里出错了?
您的支票 return 是空的 Nokogiri::XML::NodeSet
,这是真实的。
读取属性并比较其值:
if document.xpath("//records//record/@zone_transfer").first.value == 'success'
...
请注意,这仅在 xpath 至少匹配一个元素时有效(因此它不适用于 "brute" 文件)。
或添加 any?
以获得布尔值 return:
if document.xpath("//records//record[@zone_transfer='success']").any?
...
x = document.xpath("//records//record[@zone_transfer='success']")
p x
--output:--
[]
和:
if []
puts %q{It's true!}
end
--output:--
It's true.
在 ruby 中计算结果为 false 的唯一值是 false
本身和 nil
。空数组、空散列、空字符串、0
等都是 true.
相反,您可以这样做:
if document.xpath("//records//record[@zone_transfer='success']").empty?
puts "no matching elements"
#got to next xml file
else
puts "found matches"
#do your other xpath search
end
--output:--
no matching elements