【问题标题】:Running commands over multiple files and naming of new files在多个文件上运行命令并命名新文件
【发布时间】:2019-10-23 23:03:27
【问题描述】:

这是一个 bash 脚本,我有 5 个 PCAP 文件我想对它们运行这些命令,然后在它通过的每个 PCAP 文件之后将新文件命名为 flow1 flow2 flow3 flow4 flow5 我无法正确命名文件

新文件显示为 1 个长字符串 flow1 flow2 flow3 flow4 flow5 仅用于 1 个文件

flow=(flow1 flow2 flow3 flow4 flow5)
dns=(dns1 dns2 dns3 dns4 dns5 dns6)
ntp=(ntp1 ntp2 ntp3 ntp4 ntp5 ntp6)

#creates a new directory based on the PCAP folder name 
for file in *.pcap
do
argus -r *.pcap -w packet.argus &&
#Run argus to get the flow volumn (totalbytes) and the flow duration (seconds)
#ra -r packet.argus -s bytes dur > flow_vol_dur.csv
ra -r packet.argus -s bytes dur -u > "${flow[*]}.csv"

ra -r packet.argus -n -s bytes dur rate runtime pkts load loss > features.csv &&                                                                       
#Run argus to get the source and destination ports, merge both columns together and count how many occurrences
#racluster -r packet.argus -n -s sport dport > ports.csv &&
ra -r packet.argus -n -s stime ltime sport dport - dst port 53 > "${dns[*]}.csv" 
ra -r packet.argus -n -s stime ltime sport dport - dst port 123 > "${ntp[*]}.csv" &&

rm packet.argus 

done

【问题讨论】:

  • 有一些明显的问题:1)您的for 循环正在遍历当前目录中的所有*.pcap 文件并将每个文件放在${file} 变量中,但您不是在循环中的任何命令中使用该变量。 2) STDOUT 重定向运算符 (>) 采用单个文件名。您正在传递"${flow[*]}.csv",它是一个包含flow 数组所有元素的字符串。这不是你想要的。

标签: bash


【解决方案1】:

在不确切知道 argusra 命令的作用的情况下,我怀疑这更接近您的需要:

#!/bin/bash

count=0
for file in *.pcap; do
  ((count++))
  argus -r ${file} -w packet.argus
  ra -r packet.argus -s bytes dur -u > flow${count}.csv
  ra -r packet.argus -n -s bytes dur rate runtime pkts load loss > features.csv
  ra -r packet.argus -n -s stime ltime sport dport - dst port 53 > dns${count}.csv 
  ra -r packet.argus -n -s stime ltime sport dport - dst port 123 > ntp${count}.csv
  rm packet.argus 
done

【讨论】:

  • 谢谢你知道每次有新循环我都可以删除 packet.argus 文件吗?
  • 我相信packet.argus 文件是在每次查找开始时由argus 命令创建的,并且可以在每次循环结束时安全地删除,因为它位于我的第 11 行脚本。如果这是一个问题,只需将第 11 行移出循环即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2012-12-28
  • 2017-09-16
  • 2012-02-18
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多