2017-07-27 86 views
1

我试图解析一些nmap XML通过Nokogiri检索各种值,但有相当一段时间计算出递归。我想要的是抓住ip_address和具有status='open'子元素的任何port元素。我希望能够把每一组成JSON对象保存到数据库中列后显示:Nokogiri检索嵌套结果

到目前为止,这是我的代码:

require 'nokogiri' 
require 'json' 

doc = File.open("network_ports.xml") { |f| Nokogiri::XML(f) } 

doc.xpath('//host').each do |host| 
    @ip_address = host.at_xpath("address[@addrtype='ipv4']").at_xpath("@addr").value 
    puts "Found Host: #{@ip_address}" 
    host.xpath('ports/port').each do |port|  
    if port.at_xpath("state[@state='open']") 
     puts port.at_xpath("port[@portid]").value 
    end 
    end 
end 

但是我的输出被打破,现在与以下错误:

port_parser.rb:11:in `block (2 levels) in <main>': undefined method `value' for nil:NilClass (NoMethodError) 

从文件中的一些相同的输入:

<host starttime="1501187906" endtime="1501189103"><status state="up" reason="syn-ack" reason_ttl="0"/> 
<address addr="10.10.10.1" addrtype="ipv4"/> 
<hostnames> 
<hostname name="eclipse" type="PTR"/> 
</hostnames> 
<ports><extraports state="closed" count="996"> 
<extrareasons reason="conn-refused" count="996"/> 
</extraports> 
<port protocol="tcp" portid="53"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="domain" product="dnsmasq" version="2.45" method="probed" conf="10"><cpe>cpe:/a:thekelleys:dnsmasq:2.45</cpe></service></port> 
<port protocol="tcp" portid="80"><state state="filtered" reason="syn-ack" reason_ttl="0"/><service name="http" product="DD-WRT milli_httpd" hostname="eclipse" method="probed" conf="10"/></port> 
<port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" tunnel="ssl" method="table" conf="3"/></port> 
<port protocol="tcp" portid="2222"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" product="Dropbear sshd" version="0.52" extrainfo="protocol 2.0" ostype="Linux" method="probed" conf="10"><cpe>cpe:/a:matt_johnston:dropbear_ssh_server:0.52</cpe><cpe>cpe:/o:linux:linux_kernel</cpe></service></port> 
</ports> 
<times srtt="4727" rttvar="1840" to="100000"/> 
</host> 

我是否遇到问题,因为我依赖于port元素中嵌套的state元素,我需要能够从中获取portid属性值?

最后,我只是希望一些输出,如:

Found host: 10.10.10.1 
    port 22/tcp open 
    port 23/tcp open 
Found host: 10.10.10.2 
    port 443/tcp open 
    port 5432/tcp open 
etc... 

如果您有关于清理代码的任何建议,我所有的耳朵,以及!

回答

1

一切都与您的代码似乎是工作很好,除了

puts port.at_xpath("port[@portid]").value 

您在port元素是了,所以你只需要XPath的@portid

puts port.at_xpath("@portid").value 

,并与您的示例network_ports.xml,即输出

# Found Host: 10.10.10.1 
# 53 
# 443 
# 2222 
+0

啊,那是我的问题!所以从儿童'国家'元素的回归根本不是问题!谢谢 – Godzilla74

0

您是否知道NMAP gem

require 'nmap/xml' 

Nmap::XML.new('scan.xml') do |xml| 
    xml.each_host do |host| 
    puts "[#{host.ip}]" 

    host.each_port do |port| 
     puts " #{port.number}/#{port.protocol}\t#{port.state}\t#{port.service}" 
    end 
    end 
end