Export to File with > fields.csv
In this step, we'll learn how to save the extracted packet data into a CSV (Comma-Separated Values) file. CSV is a simple file format that stores tabular data in plain text, making it perfect for analysis in spreadsheet applications or data processing tools. By saving our packet data to a file, we create a permanent record that can be shared, analyzed, or processed later.
Let's combine everything we've learned so far into a single command that extracts multiple packet fields and saves them to a file:
tshark -r sample.pcap -T fields -e frame.number -e ip.src -e ip.dst -e tcp.port -E separator=, > fields.csv
Here's what each part does:
-r sample.pcap
reads our packet capture file
-T fields
tells Tshark we want field output
- Each
-e
flag specifies a field to extract (packet number, source IP, destination IP, and port)
-E separator=,
sets the comma as our field separator
> fields.csv
redirects the output to a file instead of showing it on screen
After running this command, let's check if our file was created properly:
ls -l fields.csv
head fields.csv
The ls -l
command shows file details, while head
displays the first few lines. Your output should look something like this:
1,192.168.1.1,192.168.1.2,443
2,192.168.1.2,192.168.1.1,80
3,192.168.1.3,192.168.1.4,22
To make this data more understandable, we can add column headers. Here's how to create a new file with headers and combine it with our data:
echo "Packet,Source,Destination,Port" > headers.csv
cat headers.csv fields.csv > final.csv
mv final.csv fields.csv
This creates a new version of our CSV file with descriptive headers at the top, making it much easier to work with the data in spreadsheet applications.