我正在尝试创建一个解析器,用于多个CSV文件,并最终输出到另一个与Excel兼容的CSV文件。这些CSV文件是由商业工具导出的,该工具获取防火墙配置并向我们提供找到的任何问题的报告。
到目前为止,我已经想出了如何读取文件夹中的文件,查找特定值,确定我拥有的设备类型,然后将其输出到屏幕或CSV中,但仅当每行具有单个单元格条目时。如果源IP“单元格”(或任何其他单元格)包含多个IP,通过换行符分隔,输出会在该换行符上中断,并将其余部分推送到下一行。
我目前的代码是:
到目前为止,我已经想出了如何读取文件夹中的文件,查找特定值,确定我拥有的设备类型,然后将其输出到屏幕或CSV中,但仅当每行具有单个单元格条目时。如果源IP“单元格”(或任何其他单元格)包含多个IP,通过换行符分隔,输出会在该换行符上中断,并将其余部分推送到下一行。
我目前的代码是:
require 'csv'
require 'pp'
nipperfiles = Dir.glob(ARGV[0] + '/*.csv')
def allcsv(nipperfiles)
filearray = []
nipperfiles.each do |csv|
filearray << csv
end
filearray
end
def devicetype(filelist)
filelist.each do |f|
CSV.foreach(f, :headers => true, :force_quotes => true) do |row|
if row["Table"] =~ /audit device list/ && row["Device"] =~ /Cisco/
return "Cisco"
elsif row["Table"] =~ /audit device list/ && row["Device"] =~ /Dell/
return "Sonicwall"
elsif row["Table"] =~ /audit device list/ && row["Device"] =~ /Juniper/
return "Juniper"
end
end
end
end
def adminservices(device, filelist)
administrative = []
filelist.each do |f|
CSV.foreach(f, :headers => true, :col_sep => ",", :force_quotes => true, :encoding => Encoding::UTF_8) do |row|
if row["Table"] =~ /administrative service rule/
if row["Dst Port"] != "Any" and row["Service"] != "[Host] Any"
if device == "Cisco"
administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Protocol"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Dst Port"]
elsif device == "Sonicwall"
administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Service"]
elsif device == "Juniper"
administrative << row["Table"] + ',' + row["Rule"] + ',' + row["Source"] + ',' + row["Destination"] + ',' + row["Service"]
end
end
end
end
end
administrative
end
def writecsv(admin)
finalcsv = File.new("randomstorm.csv", "w+")
finalcsv.puts("Administrative Services Table:\n", admin, "\r\n")
finalcsv.close
end
filelist = allcsv(nipperfiles)
device = devicetype(filelist)
adminservices(device, filelist)
admin = adminservices(device, filelist)
writecsv(admin)
有没有办法让它忽略单元格内的换行符,或者我的代码很糟糕,需要重新开始?
我尝试使用CSV库编写CSV文件,但结果相同,我认为这个代码稍微清晰一些,可以演示问题。
如果有帮助,我可以对输入文件进行清理。