【问题标题】:Running multiple commands in bash and creating new directories and moving csv files into the new directory在 bash 中运行多个命令并创建新目录并将 csv 文件移动到新目录中
【发布时间】:2019-12-20 22:27:09
【问题描述】:

我有几个 pcap 文件,我想在其上应用相同的命令(这些是 Argus 命令一个网络流工具)

  • 将 PCAP 文件转换为 argus 文件
  • 运行四个 Argus 命令,输出 4 个 .csv 文件
  • 基于 .pcap 文件名创建一个新目录,但只包含 pcap 名称的第一部分,例如 amazonecho_merge 我不希望合并部分作为新目录名称的一部分
  • 将所有新的 .csv 文件放入新目录

      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
      #Run argus to get the source and destination ports, merge both    columns together and count how many occurances
       racluster -r packet.argus -n -s sport dport > ports.csv
       ra -r packet.argus -s stime ltime sport dport - dst port 53 >   DNS.csv
       ra -r packet.argus -s stime ltime sport dport - dst port 123 >   NTP.csv
       dir=$(echo ${file} | awk -F. '{print $1}' OFS=.)
       mkdir $dir
      #Move all newly created .csv files to the new directory 
       mv $file*.csv $dir
      done
    

我认为命名新目录是不正确的,因为我只想要 pcap 文件的部分名称

我确信有更好的方法来运行命令,尤其是这个命令

  ra -r packet.argus -s stime ltime sport dport - dst port 53 >   DNS.csv 
  ra -r packet.argus -s stime ltime sport dport - dst port 123 >   NTP.csv

当命令只有轻微的变化时,我想知道是否有更简单的格式来运行这些命令

在 bash 中有没有办法将不同 csv 文件中的列合并到单个 csv 文件中

e.g 

      file1.csv

      A,B

      file2.csv

      C,D

      Desired output.csv

      A,B,C,D

我已尝试加入但不工作还有其他 bash 命令可以工作吗?

【问题讨论】:

  • 如果你不希望目录名中文件名的第二部分使用:dir=$(echo $file| awk -F_ '{print $1}')
  • @Tina 嗨,你能说清楚你的问题是什么吗?
  • @suspectus 嗨,你能告诉我这个问题有什么不清楚或遗漏的地方吗?我有 12 个 pcap 文件,我想运行几个 Argus 命令以及创建一个新目录,并将新创建的来自 Argus 的输出文件(.csv 文件)放入新目录
  • @Tina 只是帖子中没有问题。到目前为止,您的工作有哪些不妥之处?

标签: bash file pcap


【解决方案1】:

看起来你的大多数命令不需要每个文件运行一次,所以如果你改变顺序,你可以节省一些运行时间:

#!/usr/bin/env bash

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

# Run argus to get the source and destination ports, merge both columns together and count how many occurances
racluster -r packet.argus -n -s sport dport > ports.csv
ra -r packet.argus -s stime ltime sport dport - dst port 53 > DNS.csv
ra -r packet.argus -s stime ltime sport dport - dst port 123 > NTP.csv

for file in *.pcap
    do
    dir=$(echo $file| awk -F_ '{print $1}')
    mkdir $dir
    # Move all newly created .csv files to the new directory 
    mv $file*.csv $dir
done

【讨论】:

  • 谢谢,但如果我想在每个文件上运行 Argus 命令,我有 12 个 pcap 文件,我不需要放入 for 循环
  • 因为这是bash,你只需要dir=${file%_*}就可以得到,例如amazonecho 来自 amazonecho_merge。 (这节省了$(echo $file| awk -F_ '{print $1}') 所需的生成 4-subshel​​l)
  • 第三行运行在所有 pcap 文件上,而不是一次一个。
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多