TCPdump script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TCPdump script
# 1  
Old 03-15-2011
TCPdump script

I'm new to the Unix/Linux world. I have taken classes and played with a few simple scripts but never had a real world application. Here is my problem.

What I need to do is every 15min between 8am and 5pm, run
tcpdump -s 2000 -w flowroute-0000.pcap

where the "0000" is the current time.

I understand i should be able to do some thing like this. Not sure if that would be a C basic, or bash shell... not sure.
This will be running on a VoIP server, Trixbox pro call center edition.
this is basically a lunix build with built in VoIP functions.
Thanks in advance for reading.
# 2  
Old 03-15-2011
cron can do the timing. The tricky thing is the date command % metacharacter is also the cron metacharacter, so you need to write a trivial script to call from cron. Do you want the date as well, or will it overwrite daily?
Code:
----- tcpd_cron.ksh file (#! must be on first line, chmod 744) -----
#!/usr/bin/ksh
 
(
date "+=============================
%Y-%m-%d %H:%M:%S Start ${0##*/} ($$)."
 
. ./.profile
 
tcpdump -s 2000 -w flowroute-$( date '+%H''%M' ).pcap
 
date "+%Y-%m-%d %H:%M:%S End ${0##*/} ($$) tcpdump returned $?.
==================================="
) >>tcpd_cron.log 2>&1
 
----- your crontab line ------
0,15,30,45 8-16 * * * tcpd_cron.ksh

The two single quotes in the middle of the date option is because %H% is an SCCS meta-string! Files are in your home dir, unless you expand the path! I assume your .profile will not mind running from a non-tty session (put all the term stuff at the end in an if) and sets any necessary PATH and such. As I recall, in cron stderr is discarded and stdout is emailed to you, so you want to redirect the outputs to a log file and log start and stop of every run with a date command, as bookends. A subshell does this once for everything inside.

Last edited by DGPickett; 03-15-2011 at 03:56 PM..
# 3  
Old 03-15-2011
What worries me is by the end of the day you'll have a bunch of tcpdump processes running, unless you go in and kill them off.

tcpdump might do what you want it to do, needing a script only to start it once per day and stop it once per day. Check 'man tcpdump', specifically for the "-G" and "-w" options.

Also beware logging that much traffic on a busy system. There's no guarantee that you catch every packet, and the chance that you fill you filesystem in short order.

Edit: It looks like the -G option doesn't show up until version 4.0.0 - something like this should work in that version, but I can't test it at the moment:
Code:
# tcpdump -s2000 -w'flowroute-%H%M.pcap' -G900 -W36

Then you'd just need to schedule it to start at 08:00, and it should finish on its own at 17:00. As pointed out above, you'll want to wrap it in a small script for the date handling and to log the output.

Last edited by LivinFree; 03-15-2011 at 05:15 PM..
# 4  
Old 03-15-2011
Thanks for all the feed back. I will need to look into using cron, not some thing i have used before but I understand the idea. I was thinking that i could use that for timing, thank you for the cron lines.
0,15,30,45 8-16 * * * tcpd_cron.ksh
As for the date, I dont need the date. These files will be downloaded at the end of each work day.
My original plan was to just start the cron job at the beginning of the day and stop it at end of the work day. Then download all the data off the server to save space. This way I can maintain a log of all the calls.

My biggest worry is that the VoIP server might be a little taxed from the ongoing processes running. I don't have a test server I can use at the moment but Im working on building one.
# 5  
Old 03-16-2011
Requirement creep. Cron has a column for day of the week, but since you were overwriting, it seemed ok to run it every day regardless. If you want to keep, add the date into the file name.

I am not a tcpdump user this decade, so I admit you do need to stop it somehow. You might use head to cap the byte count and spin it off in the background, sleep a while, and 'kill -9 $! 2>/dev/null', so it does not run into the next run.

You might even put the files in a zip for each week or month, so space is not so quickly threatened, they are easily discarded and the dir does not grow big.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcpdump on many machines from single script

Hi all, new to the forum and rusty with my scripting. I am trying to put together a quick and dirty script that will kickoff a tcpdump on multiple machines. Then, another script that will reach out (at a later time) to stop the processes and retrieve the data. It seems fairly easy conceptually... (2 Replies)
Discussion started by: k9sar
2 Replies

2. IP Networking

TCPdump

I've recently started learning to use TCPdump, and I find it pretty interesting. There's one thing I don't understand. When I tell it to capture packets on, say, the WiFi interface en1, it often captures packets sent or received by other hosts on the network. How can it do this? My... (3 Replies)
Discussion started by: Ultrix
3 Replies

3. Debian

Tcpdump Help !

Hi. Need Help with TcpDump Trying to sniff associatio-request with tcpdump but when i run this tcpdump -i eth0 wlan subtype assoc-req i get this error can anyone help me with this error ? Thanks alot !!:) (1 Reply)
Discussion started by: SoulZB
1 Replies

4. Shell Programming and Scripting

tcpdump script to parse "packers captured" details

I want a script that would do as:- a) gives me packet capture account for each time it runs. b) be able to run at a particular time for specific period time duration (1 min). c) for each time it runs it saves the time / day. Is there a way where i can capture the details as seen in the... (2 Replies)
Discussion started by: lazerz
2 Replies

5. Shell Programming and Scripting

write a script to parse some tcpdump output

i am trying to write a script to parse some tcpdump output, in each line of the tcpdump output, I know for sure there are 3 keywords exist: User{different usernamehere} NAS_ipaddr{different ip here} Calling_station{ip or dns name here} But the positions for these 3 keywords in the... (4 Replies)
Discussion started by: fedora
4 Replies

6. Shell Programming and Scripting

Help with script, trying to get tcpdump and rotate the file every 300 seconds

Greetings, I just started using scripting languages, im trying to get a tcpdump in a file, change the file name every 5mins ... this is what i have but its not working ... any suggestions? #!/bin/bash # timeout.sh #timestamp format TIMESTAMP=`date -u "+%Y%m%dT%H%M%S"` #tdump =`tcpdump... (3 Replies)
Discussion started by: livewire
3 Replies

7. IP Networking

i would like to know about tcpdump

i would like to know about tcpdump i would like to use tcpdump to get information about these - Date - time - source hostname - source mac address - source ip address - destination ip address - see outbound only then i use command like this tcpdump -i le0 -n -q -tttt -e src net... (2 Replies)
Discussion started by: chamnanpol
2 Replies

8. Cybersecurity

i would like to know about tcpdump

i would like to know about tcpdump i would like to use tcpdump to get information about these - Date - time - source hostname - source mac address - source ip address - destination ip address - see outbound only then i use command like this tcpdump -i le0 -n -q -tttt -e src net... (0 Replies)
Discussion started by: chamnanpol
0 Replies

9. UNIX for Dummies Questions & Answers

tcpdump

does anybody know what the -d -dd and -ddd options are used for ? thanks (2 Replies)
Discussion started by: ant04
2 Replies

10. Programming

How To Use tcpdump

I have two net-card. one is 172.16.24.99(ENG) ,another is 172.16.25.99(ENG-B). Both masks is 255.255.255.0. I will monitor data on the tcp port 8055 in ENG, How do I set option of tcpdump command (2 Replies)
Discussion started by: chenhao_no1
2 Replies
Login or Register to Ask a Question