LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2011, 06:15 AM   #1
daljian
LQ Newbie
 
Registered: Nov 2011
Location: Karlstad, Sweden
Posts: 3

Rep: Reputation: Disabled
Smile Analysing libpcap capture files automatically/CLI


Hi,
I've been coming here many times looking for answers, and it's proven to be a really useful source of information.

Now I have a question on my own which I'm hoping you guys have some ideas about

Really, what I want to do is to capture network traffic continuously so that I keep a day or so of traffic which can be analysed manually.

In addition to this, if I'm looking for something in particular. Ie, I want to find a SIP packages containing a specific header value or something else.
Basically, what I would normally use as display filter in wireshark I want to be able to do in a command line way to see if filter will show zero or more packages.

A bonus would be if I could get text representation of packages that are matched, but that should not be needed.

Any ideas?

BR
Göran
 
Old 11-04-2011, 08:29 AM   #2
daljian
LQ Newbie
 
Registered: Nov 2011
Location: Karlstad, Sweden
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi,
I found that tshark can do what I need.
It supports display filters with the -R flag as the example below.

tshark -R "http.proxy_connect_host == "id.google.com"" -r /tmp/sample.pcap
 
Old 11-08-2011, 04:38 AM   #3
daljian
LQ Newbie
 
Registered: Nov 2011
Location: Karlstad, Sweden
Posts: 3

Original Poster
Rep: Reputation: Disabled
End result in case someone is interested.

Code:
#!/bin/bash
#
# Configuration
#
#This script allows you to do an automated analysis based on display filter.

#path to save trace
TRACES_DIR=/tmp


HOSTNAME=`hostname`
FILE_NAME="${TRACES_DIR}/traffic_${HOSTNAME}.cap"
FILE_FILTER="${TRACES_DIR}/traffic_${HOSTNAME}*.cap"

#intervals in seconds
CAPTURE_INTERVAL=10
#Keep Analyse interval less than capture interval
let "ANALYSE_INTERVAL=${CAPTURE_INTERVAL} - 1"

#Ie, if you want to save all capture files that has to do with
# http traffic containing the phrase "tbg.nu" you can use the below
# For more on display filters, please have a look at:
# http://www.wireshark.org/docs/dfref/
DISPLAY_FILTER="http contains tbg.nu"
CAPTURE_FILTER="port 80"
MATCH_CRITERIA=".*"
MATCH_ACTION="/bin/gzip"
NO_MATCH_ACTION="/bin/rm"

function analyse
{
	capture_file=$(ls -tr ${FILE_FILTER} | tail -2 | head -1)
	number_of_captures=$(ls ${FILE_FILTER} | wc -l)
	chmod 666 $capture_file
	if [ ! ${capture_file}0 = "0" -a ${number_of_captures} -gt 1 ]; then
		matches=$(tshark -R "${DISPLAY_FILTER}" -r ${capture_file} | grep -c -e"${MATCH_CRITERIA}")
		if [ $matches -gt 0 ]; then
		  $MATCH_ACTION $capture_file
		else
		  $NO_MATCH_ACTION $capture_file
		fi
	fi
}
function capture
{
	#Let's capture 
	# tcp dump version: tcpdump -s 0 -C 12 -W 10  -i any -w /tmp/traffic
	sudo tshark -i any -b duration:${CAPTURE_INTERVAL} -w $FILE_NAME $CAPTURE_FILTER > /dev/null 2>&1
}
function checkroot
{
	if [ "$(id -u)" != "0" ]; then
		echo "This script must be run as root" 1>&2
	exit 1
fi
}


## Script start
checkroot
capture &
while [ true ]
do
   sleep ${ANALYSE_INTERVAL}
   analyse
done

exit 0
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"cannot stat `libpcap.so.1.0.0':" error during make of Libpcap 1.0.0 on Fedora 9 myriad_moments Linux - Software 4 04-09-2010 02:02 AM
Compiling libpcap files Mridulj Linux - Software 10 04-12-2009 03:24 PM
Can you have "sudo" automatically added to each line in the CLI? Free_beer Ubuntu 7 09-29-2006 03:59 PM
capture audiostream automatically h2gofast Linux - Software 5 06-23-2005 01:06 PM
how can i use libpcap to capture packets for the local loopback interface (lo) ? gajaykrishnan Programming 0 02-22-2005 04:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration