[英]Ruby-On-Rails Report creating via accessible ports
host = Host.find(i)
a = host.open_ports
openPorts = []
a.split(",").each do |x|
openPorts << x
end
這是在談論Ruby on Rails的設置,因此我在i
上設置了我的Ip地址並獲取了所有打開的端口。然后,返回的字符串通過","
分解","
並添加到數組中。
Finding.all.each do |p|
openPorts.each do |y|
if p.port == y
Report.create(:port => p.port,
:risk_rating => p.risk_rating,
:finding => p.finding,
:implication => p.implication,
:recommendation => p.recommendation)
end
end
end
遍歷數據庫中的發現表,並檢查端口是否與我們上面創建的開放端口數組匹配。 如果存在匹配項,我們將根據查找表中的給定值創建一個新報告。
問題是即使有匹配項,也不會創建新報告。
任何幫助表示贊賞。
不確定這是否有幫助,但是我想向您展示如何清理實現。
host = Host.find(i)
# I am assuming Finding#port is an Integer
# if Finding#port is a String then just remove the .map(&:to_i) portion
open_ports = host.open_ports.split(",").map(&:to_i)
Finding.where(port: open_ports).each do |p|
Report.create(:port => p.port,
:risk_rating => p.risk_rating,
:finding => p.finding,
:implication => p.implication,
:recommendation => p.recommendation)
end
讓我們從頂部開始
String#split
返回一個數組,因此無需將其推入新數組。 但是,它確實創建了一個字符串數組,因此如果您需要整數,則#map(&:to_i)
會為您完成此操作。 我假設這是當前問題,它是將字符串與整數進行比較,例如"80" == 80 #=> false
接下來而不是遍歷所有Finding
為什么不只拔出具有匹配端口的端口呢? Finding.where(port: open_ports)
這將生成一個查詢,例如SELECT findings.* FROM findings where findings.port IN (YOUR_OPEN_PORTS_ARRAY)
Finding.where(port: open_ports)
SELECT findings.* FROM findings where findings.port IN (YOUR_OPEN_PORTS_ARRAY)
然后,我們僅從此受限列表創建報告,而不是遍歷所有Finding
的循環,然后遍歷open_ports
的循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.