Get number of incoming connections on specific port with ss
Published: 25-08-2020 | Author: Remy van Elst | Text only version of this article
Recently I had to write a few monitoring plugins, one of which was a count of
incoming established connections to a specific network port. In the past I
would have used
netstat and a combination of
wc to filter out only
specific ports and established connections, but nowdays
netstat is replaced by
ss on ubuntu.
ss has options to filter directly on all sorts of stuff, like
state, ports, protocol, making the command I use more readable and use less
Here's an example of the graph my code makes:
The below snippet is from one of my webservers. You can check the manpage for more information, they have another quite elaborate example:
ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24 List all the tcp sockets in state FIN-WAIT-1 for our apache to network 193.233.7/24 and look at their timers.
connection filtering with ss
This is the command, it even works for non-root users, which was nice since my monitoring plugins do not run as root.
ss --all --numeric --no-header state established '( sport = :443 )'
tcp 0 0 184.108.40.206:443 220.127.116.11:55467 tcp 0 0 18.104.22.168:443 22.214.171.124:39760 tcp 0 0 126.96.36.199:443 188.8.131.52:57065
To count, just add
ss --all --numeric --no-header state established '( sport = :443 )'| wc -l
Change the port or add more filters,
'( sport = :443 or sport = :80 )' (mind
the trailing space after the port number), you should be able to figure it out.
On a system that still has
netstat, I use the following command:
Tags: bash , monitoring , munin , netstat , networking , shell , snippets , ss
netstat -anp | grep :443 | grep ESTABLISHED | wc -l