You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将tcptrack的输出保存到文本文件?已尝试grep和cut但无效

Got it, I totally get why grep/cut aren't working here—tcptrack uses curses to render that dynamic, updating terminal interface, which means regular redirection or pipe tools can't properly capture the changing screen content. Let's walk through a few reliable solutions to save its output to a text file:

Solution 1: Use the script Command (Easiest Approach)

The script tool is built specifically to record terminal sessions, including output from curses-based programs like tcptrack. It captures every bit of what's displayed in the terminal, even the dynamic updates.

  • Start a script session that runs tcptrack directly, specifying your output file:
    script -c "tcptrack -i eth0" tcptrack_output.txt
    
    Replace eth0 with your network interface name, and tcptrack_output.txt with whatever filename you want to save to.
  • When you're done capturing, press Ctrl+C to stop tcptrack—script will automatically end the session and save all output to your file.
  • To view the output properly (since it might include curses control characters), use less -R or cat -v:
    less -R tcptrack_output.txt
    
    The -R flag tells less to interpret color and formatting escape codes correctly.
Solution 2: Capture Traffic with tcpdump + Analyze Later (For Data-Only Needs)

If you don't need the exact dynamic interface of tcptrack, just the connection and traffic statistics it shows, using tcpdump to capture packets and then analyzing them with a tool like tshark is a more flexible approach.

  • Capture packets to a file first:
    tcpdump -i eth0 -w tcptrack_capture.pcap
    
  • Stop the capture with Ctrl+C, then analyze the pcap file to get connection stats similar to tcptrack:
    tshark -r tcptrack_capture.pcap -q -z conv,tcp
    
    This outputs a clean, static list of TCP connections with byte counts—you can redirect this directly to a file with > tcp_connections.txt.
Solution 3: Use strace to Capture Output (More Technical)

If you want to directly capture the output tcptrack sends to the terminal, you can use strace to trace its write system calls. This skips the curses terminal control layer and grabs the raw output.

  • Run tcptrack with strace to log write operations to stdout/stderr:
    strace -e write=1,2 -o tcptrack_strace.txt tcptrack -i eth0
    
  • Clean up the output file to remove strace's metadata and extract the actual tcptrack content:
    grep -o '"[^"]*"' tcptrack_strace.txt | tr -d '"' > tcptrack_cleaned.txt
    
    This will give you a plain-text version of the output, though you might need to do some extra trimming to remove leftover control characters.

Quick Recommendation

Stick with the script method if you want to preserve the full dynamic interface of tcptrack. If you only care about the underlying connection data, tcpdump + tshark is cleaner and easier to work with for post-processing.

内容的提问来源于stack exchange,提问作者Rohit_Srivastav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:13:44