使用 Ruby 解析和显示 XML

问题描述

我需要(想要)使用 ruby​​ 脚本解析 nessus 输出(XML),并最终将其输出到 CSV 和 HTML 文件。我是 Ruby 的新手,所以在我学习的时候,我会对终端的一些输出感到满意。下面是 nessus 输出的示例和我迄今为止构建的脚本。该脚本不会按原样返回任何错误,但也不会返回我需要的任何数据。有人能指出我正确的方向吗?

</nessusClientData_v2>
</Report>
</ReportHost> 
<ReportItem port="0" svc_name="general" protocol="tcp" severity="0" pluginID="12053" pluginName="Host Fully Qualified Domain Name (FQDN) Resolution" pluginFamily="General">
<description>nessus was able to resolve the FQDN of the remote host.</description>
<fname>fqdn.nasl</fname>
<plugin_modification_date>2012/09/28</plugin_modification_date>
<plugin_name>Host Fully Qualified Domain Name (FQDN) Resolution</plugin_name>
<plugin_publication_date>2004/02/11</plugin_publication_date>
<plugin_type>remote</plugin_type>
<risk_factor>None</risk_factor>
<script_version>$Revision: 1.12 $</script_version>
<solution>n/a</solution>
<synopsis>It was possible to resolve the name of the remote host.</synopsis>
<plugin_output>
12.233.108.201 resolves as preprod.boardvantage.net.
</plugin_output>
</ReportItem>
<ReportItem port="443" svc_name="https?" protocol="tcp" severity="0" pluginID="11219" pluginName="nessus SYN scanner" pluginFamily="Port scanners">
<description>This plugin is a SYN &apos;half-open&apos; port scanner.
It shall be reasonably quick even against a firewalled target.

Note that SYN scanners are less intrusive than TCP (full connect) scanners against broken services,but they might kill lame misconfigured firewalls. They might also leave unclosed connections on the remote target,if the network is loaded.</description>
<fname>nessus_syn_scanner.nbin</fname>
<plugin_modification_date>2011/04/05</plugin_modification_date>
<plugin_name>nessus SYN scanner</plugin_name>
<plugin_type>remote</plugin_type>
<risk_factor>None</risk_factor>
<script_version>$Revision: 1.14 $</script_version>
<solution>Protect your target with an IP filter.</solution>
<synopsis>It is possible to determine which TCP ports are open.</synopsis>
<plugin_output>Port 443/tcp was found to be open</plugin_output>
</ReportItem>
</ReportHost>
</Report>
</nessusClientData_v2>

nessusParse.rb

#!/usr/bin/ruby

require 'nokogiri'

nessus_to_parse = gets

doc = Nokogiri::XML(nessus_to_parse)
doc.xpath('//ReportItem').each do |i|
  puts "Host = " + i.at_xpath('name').content
  puts "Name = " + i.at_xpath('plugin_name').content
  puts "Severity = " + i.at_xpath('risk_factor').content
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)