Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Want an option to exit after specified time #338

Open
sonots opened this issue Oct 31, 2013 · 7 comments
Open

[Feature] Want an option to exit after specified time #338

sonots opened this issue Oct 31, 2013 · 7 comments
Assignees

Comments

@sonots
Copy link

sonots commented Oct 31, 2013

I want an option to let tcpdump exit after a specified time passed (seconds, minutes, hours).

The reason is because I want to start tcpdump only during a specific time like 10:00 - 13:00. I often use crontab to start tcpdump on a specific time (like 10:00) with -c option to let tcpdump automatically die, but I can not assure it will not die until a specific time (like 13:00 here) and will die after the specific time.

Especially, if the captured server is service-out, it will not receive packets any more, then the tcpdump lives forever. I want to avoid such situations, too

@infrastation
Copy link
Member

It is possible to have one cron job to start tcpdump and write a pidfile and another cron job to stop tcpdump.

@sonots
Copy link
Author

sonots commented Oct 31, 2013

Right, but I felt the new option is more useful than -c option.

@mcr
Copy link
Member

mcr commented Oct 31, 2013

I agree that an option to exit after some period of time, or after writing X many save files, and it would also be nice to have an option to roll the save file after X minutes rather than on Z bytes.
My goal in tcpdump is to create a parallel program that uses the same print-*.c files, but is called pktdump; it would have a fresh set of (long) options. That's why all the ndo stuff... maybe you want to suggest an interface to specify the options, sonots?

Here is my script to start/stop tcpdump, that I use in production:

#!/bin/sh

set -ex
PATH=/usr/sbin:/sbin:/usr/bin:/bin:$PATH export PATH
TRACEIF=$1
TRACEFN=$1-$(date +%s)
TRACEDIR=$(date +/traces/%Y/%m/%d)

(

mkdir -p $TRACEDIR

cd $TRACEDIR
if [ -r /traces/${TRACEIF}.pid ]; then
    olddumppid=$(cat /traces/${TRACEIF}.pid )
fi
sh -c 'echo $$ >/traces/'$TRACEIF'.pid ; exec tcpdump -i '$TRACEIF' -s 0 -n -C 16 -w '${TRACEFN}'.pcap ' </dev/null >$TRACEIF.out 2>&1 &

if [ -n "$olddumppid" ]; then
    echo Killing old tcpdump for $TRACEIF at $olddumppid. | Mail -s "$TRACEIF restarted" [email protected]
    kill $olddumppid
fi

) &

@ghost ghost assigned mcr Oct 31, 2013
@infrastation
Copy link
Member

Let's have this issue assigned to @mcr then.

@jaunix
Copy link

jaunix commented Mar 16, 2015

This seems like a fairly trivial feature to add, master...jaunix:stop-after-time

 $ ./tcpdump -h
 tcpdump version 4.8.0-PRE-GIT_2015_03_16
 libpcap version 1.8.0-PRE-GIT_2015_03_07
 OpenSSL 1.0.1e-fips 11 Feb 2013
 Usage: tcpdump [-aAbdDefhHIJKlLnNOpqRStuUvxX#] [ -B size ] [ -c count ]
                 [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
                 [ -i interface ] [ -j tstamptype ] [ -M secret ] --limit-time seconds] [ --number ]
                 [ -Q in|out|inout ]
                 [ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]
                 [ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]
                 [ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z command ]
                 [ -Z user ] [ expression ]
 $ date ; sudo ./tcpdump --limit-time 3 -w /tmp/foo ; date
 Mon Mar 16 15:53:44 PDT 2015
 tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
 5 packets captured
 5 packets received by filter
 0 packets dropped by kernel
 Mon Mar 16 15:53:49 PDT 2015
 $

@gvanem
Copy link
Contributor

gvanem commented Mar 17, 2015

You want to do it with a call to signal(SIGALRM,cleanup) + alarm()? MSVC/MingW doesn't have that. There must be a cleaner way to do this.

@zhexuany
Copy link

zhexuany commented Dec 14, 2016

The alternative approach I found is to use timeout if you are using Linux. The delta of time different is around 0.02. I am pretty fine with this approach unless tcpdump proposes a native implementation.
Say I only want tcpdump listen with 15 seconds. I can do the following.

timeout 15 tcpdump -i any -s 0 port 3000 tcp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants