Home >Operation and Maintenance >Linux Operation and Maintenance >Example analysis of port traffic statistics under Linux
This article mainly introduces how to count port traffic under Linux. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
To monitor and count the program’s exposed port traffic without modifying the source code, you can use the Iptable that comes with Linux to add simple rules to allow it to perform port traffic statistics. role. However, it should be noted that the statistics will be reset to zero when the server is restarted and the Iptable service is restarted.
Add the port that needs statistics
1. Input monitoring
The following example is to monitor the input traffic whose target port is 8080--dport(destination port Abbreviation of source port)
iptables -A INPUT -p tcp --dport 8080
2. Output monitoring
The following example is to monitor the output traffic whose source port is 8080--sport(abbreviation of source port)
iptables -A OUTPUT -p tcp --sport 8080
View statistics
iptable -L -v -n -x
Example results:
The traffic received by port 8080 is 2885 bytes, and the traffic sent is 8240 bytes
Chain INPUT (policy ACCEPT 202 packets, 25187 bytes) pkts bytes target prot opt in out source destination 18 2885 tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 184 packets, 45774 bytes) pkts bytes target prot opt in out source destination 12 8240 tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:8080
Heavy Reset statistics
Note: Here are the statistics for resetting all ports
1. Reset all input ports
Iptable -Z INPUT
2. Reset all output ports
Iptable -Z OUTPUT
Remove statistics port
1. Remove input port
iptables -D INPUT -p tcp --dport 8080
2. Remove output port
iptables -D OUTPUT -p tcp --sport 8080
The above is the detailed content of Example analysis of port traffic statistics under Linux. For more information, please follow other related articles on the PHP Chinese website!