Skip to content

Tshark

CLI 에서 사용 가능한 Wireshark

Install

sudo apt install tshark

Usage

서버에 설치된 NIC 출력:

tshark -D

패킷 자르기

editcap 으로 자르는 방법도 있긴 하다.

시간 단위로 분할할 때 파일명을 "%Y%m%d_%H%M.pcap" 으로 하는 방법

WARNING

GPT 질문 결과임. 검증 필요.

#!/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