清单 4.netstat 实用程序的用法模式
View all TCP sockets currently active$ netstat --tcpView all UDP sockets$ netstat --udpView all TCP sockets in the listening state$ netstat --listeningView the multicast group membership information$ netstat --groupsDisplay the list of masqueraded connections$ netstat --masqueradeView statistics for each protocol$ netstat --statistics |
尽管存在许多其他的实用程序,但 netstat 的功能很全面,它覆盖了 route、ifconfig 和其他标准 GNU/Linux 工具的功能。
监视流量
可以使用 GNU/Linux 的几个工具来检查网络上的低层流量。tcpdump 工具是一个比较老的工具,它从网上“嗅探”网络数据包,打印到 stdout 或记录在一个文件中。该功能允许查看应用程序产生的流量和 TCP 生成的低层流控制机制。一个叫做 tcpflow 的新工具与 tcpdump 相辅相成,它提供协议流分析和适当地重构数据流的方法,而不管数据包的顺序或重发。清单 5 显示 tcpdump 的两个用法模式。
清单 5.tcpdump 工具的用法模式
Display all traffic on the eth0 interface for the local host$ tcpdump -l -i eth0Show all traffic on the network coming from or going to host plato$ tcpdump host platoShow all HTTP traffic for host camus$ tcpdump host camus and (port http)View traffic coming from or going to TCP port 45000 on the local host$ tcpdump tcp port 45000 |
tcpdump 和 tcpflow 工具有大量的选项,包括创建复杂过滤表达式的能力。查阅下面的 参考资料 获取更多关于这些工具的信息。
tcpdump 和 tcpflow 都是基于文本的命令行工具。如果您更喜欢图形用户界面(GUI),有一个开放源码工具 Ethereal 也许适合您的需要。Ethereal 是一个专业的协议分析软件,它可以帮助调试应用层协议。它的插入式架构(plug-in architecture)可以分解协议,比如 HTTP 和您能想到的任何协议(写本文的时候共有 637 个协议)。