Network packet capture tool that runs tcpdump on configured interfaces, compresses rotated capture files with bzip2, and uploads them to S3-compatible storage (Backblaze B2).
- Go 1.22+
tcpdumpbzip2- Root privileges (for packet capture)
go build -o cloud_pcap .Copy config.example.json to config.json and edit:
{
"interfaces": ["ens256", "ens224"],
"capture_dir": "/pcaps",
"max_file_size_mb": 1024,
"bpf_filter": "",
"snap_len": 0,
"s3": {
"endpoint": "s3.eu-central-003.backblazeb2.com",
"region": "eu-central-003",
"bucket": "company-name-traffic",
"prefix": "traffic/",
"access_key_id": "YOUR_KEY",
"secret_access_key": "YOUR_SECRET"
},
"delete_after_upload": true
}| Field | Description | Default |
|---|---|---|
interfaces |
Network interfaces to capture | (required) |
capture_dir |
Directory for pcap files | /pcaps |
max_file_size_mb |
Rotate when pcap file reaches this size in MB | 1024 |
bpf_filter |
BPF filter expression (e.g. "tcp port 80") |
(none) |
snap_len |
Snapshot length in bytes (0 = default 262144) | 0 |
s3.endpoint |
S3-compatible endpoint host | (required) |
s3.region |
S3 region | us-east-1 |
s3.bucket |
S3 bucket name | (required) |
s3.prefix |
Key prefix for uploaded files | (none) |
s3.access_key_id |
S3 access key | (required) |
s3.secret_access_key |
S3 secret key | (required) |
delete_after_upload |
Delete compressed file after successful upload | false |
# Start capture (requires root for tcpdump)
sudo ./cloud_pcap --config config.jsonThe tool will:
- Start
tcpdumpon each configured interface - Monitor capture file size, rotate when it reaches
max_file_size_mb - Compress completed files with
bzip2 - Upload
.pcap.bz2files to S3 - Optionally delete local files after successful upload
- For each interface, a goroutine runs a capture loop: start tcpdump, poll file size, stop tcpdump when the limit is reached, then compress+upload in the background and immediately start a new tcpdump
- Compression and upload run asynchronously so capture is not interrupted
- On SIGINT/SIGTERM, all tcpdump processes are stopped and pending uploads finish before exit
sudo cp cloud_pcap /usr/local/bin/
sudo mkdir -p /etc/cloud_pcap
sudo cp config.json /etc/cloud_pcap/config.jsonCreate /etc/systemd/system/cloud_pcap.service:
[Unit]
Description=Cloud PCAP Capture
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/cloud_pcap --config /etc/cloud_pcap/config.json
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now cloud_pcap