【发布时间】:2017-10-19 09:46:08
【问题描述】:
我需要使用 tcpdump 监控网络。但是,我需要每 5 秒手动从其文件夹中删除其输出(train.txt)。我正在寻找一种在 bash 文件中重新生成输出的方法。换句话说,当我删除 tcpdump 的输出时,它会生成一个包含新内容的新输出。这是我的代码:
#!/bin/bash
FILE="/media/sf_sharedsaeed/train.txt"
#Change the eth for each host first
tcpdump -i h1-eth0 -l -e -n 'port 80' & > /media/sf_sharedsaeed/train.txt
while true
do
if [ -f $FILE ]; then
echo "******************* tcpdump do its job well******************* "
else
#now interrupt the process. get its PID:
pid=$(ps -e | pgrep tcpdump)
echo $pid
kill -2 $pid
echo "File $FILE does not exist. tcpdump make it again."
tcpdump -i h1-eth0 -l -e -n > /media/sf_sharedsaeed/train.txt
fi
done
问题是,随着输出被删除,不再生成新的输出文件。请帮我。
更新: 我使用此代码删除文件内容而不是删除文件:(使用 cat /dev/null > $FILE)
#!/bin/bash
FILE="train.txt"
second=5
while true
do
awk '{print $10" "$12}' $FILE | sort | uniq -c > output.txt
echo "New output.txt generated."
#truncate -s 0 $FILE
cat /dev/null > $FILE
echo "wait for 5 seconds ..."
sleep $second
done
当我使用它时,输出(train.txt)没有完全填充。有些数据会丢失!
【问题讨论】: