Skip to content
Search

Tshark

lastmod 2024-11-27created 2024-05-233 edits

Wireshark usable from the CLI

Install

sudo apt install tshark

Usage

List the NICs installed on the server:

tshark -D

Splitting packets

Splitting with editcap is also an option.

Naming files "%Y%m%d_%H%M.pcap" when splitting by time

WARNING

This is the answer to a question asked to GPT. Needs verification.

#!/bin/bash

input_file="input.pcap"  # 원본 PCAP 파일
output_prefix="split_"   # 출력 파일의 접두사
time_interval=3600       # 시간 간격(초 단위)

# tshark로 첫 패킷의 시간 정보 가져오기
start_time=$(tshark -r "$input_file" -T fields -e frame.time_epoch | head -n 1)

# 첫 시작 시간 변환
current_time=$start_time
while :; do
    # 출력 파일 이름 생성 (시간 형식: %Y%m%d_%H%M.pcap)
    output_file=$(date -d "@$current_time" +"${output_prefix}%Y%m%d_%H%M.pcap")

    # 현재 시간부터 1시간 동안의 패킷을 필터링하여 저장
    next_time=$((current_time + time_interval))
    tshark -r "$input_file" -Y "frame.time_epoch >= $current_time && frame.time_epoch < $next_time" -w "$output_file"

    # 파일 크기가 0이면 삭제
    if [ ! -s "$output_file" ]; then
        rm "$output_file"
    fi

    # 다음 시간으로 업데이트
    current_time=$next_time

    # 파일 끝인지 확인
    if (( $(echo "$current_time > $(tshark -r "$input_file" -T fields -e frame.time_epoch | tail -n 1)" | bc -l) )); then
        break
    fi
done

Examples

tshark -i 1 -Y 'tcp.port == 80 && ip.addr == 192.168.1.10' -t a --color

See also

Favorite site

author Zer0 sitemap