【问题标题】:I want to capture daily PCAP using Tcpdump using cron我想使用 cron 使用 Tcpdump 捕获每日 PCAP
【发布时间】:2023-03-14 05:00:02
【问题描述】:

我想在 cron (Raspberry Pi) 中使用 Tcpdump 捕获 PCAP 文件。我写了一个 bash 文件。但是,我想每天在一个新文件中捕获流量并停止旧的捕获。目前创建了一个新文件,但旧文件继续捕获

 #Variable
 DATE=$(date '+%Y-%m-%d_%H%M%S')
 SET_INTERFACE=eth0
 SAVE_IN_FOLDER=/media/pi/tinausb
 SAVE_AS_FILE=tcpdump_$DATE.pcap

 #Execute tcpdump command
 /usr/sbin/tcpdump -G 86400 -i $SET_INTERFACE -s0 -w        
 SAVE_IN_FOLDER/$SAVE_AS_FILE"

【问题讨论】:

    标签: bash cron tcpdump


    【解决方案1】:
       -G rotate_seconds
          If specified, rotates the dump file specified with the -w option
          every  rotate_seconds  seconds.   Savefiles  will  have the name
          specified by -w which should include a time format as defined by
          strftime(3).  If no time format is specified, each new file will
          overwrite the previous.
    
          If used in conjunction with the -C option, filenames  will  take
          the form of `file<count>'.
    

    这意味着你用 -w 给出的文件名必须包含你传递给 date 的 % 格式。简而言之:

    SAVE_AS_FILE=tcpdump_%Y-%m-%d_%H%M%S.pcap
    

    哦,顺便说一句,您的问题与 Python 无关。

    【讨论】:

    • 正如您在 bash 文件中看到的,我已经指定了时间格式。我想我需要在新捕获开始时终止最后一个 tcpdump 进程。我没有按大小捕获我想捕获 24 个流量 - 新文件 - 然后在第二天捕获开始时停止
    • 您没有在SAVE_AS_FILE 中指定时间格式,因为$DATE 扩展为特定 日期,因此不是日期说明符。相反,使用@Tey 建议的格式,即:SAVE_AS_FILE=tcpdump_%Y-%m-%d_%H%M%S.pcap
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 2020-10-03
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2021-04-24
    • 2016-06-28
    相关资源
    最近更新 更多