Skip to content

How to calculate TCP connection infomation without ss and netstat

homepage-banner

Introduction

When troubleshooting network issues, one of the common tasks is to check the number of TCP connections. The ss and netstat commands are usually used for this purpose, but there may be situations where these commands are not available or not permitted to use. In this blog post, we will discuss how to calculate TCP connection without ss and netstat commands.

What is /proc/net/tcp?

One way to check for TCP connections is by using the /proc/net/tcp file. This file contains information about all the TCP connections on the system. To check for the number of TCP connections, we can count the number of lines in the /proc/net/tcp file.

The /proc/net/tcp file is a virtual file system that provides detailed information about the TCP connections. It contains a list of all the active TCP connections, along with their state, local and remote IP addresses, and port numbers.

cat /proc/net/tcp | wc -l

sort by remote port

array=($(tail -n +2 /proc/net/tcp | cut -d":" -f"4"|cut -d" " -f"1"))
for port in ${array[@]}; do echo $((0x$port)); done | sort | uniq -c

sort by local port

array=($(tail -n +2 /proc/net/tcp | cut -d":" -f"3"|cut -d" " -f"1"))
for port in ${array[@]}; do echo $((0x$port)); done | sort | uniq -c

Other Options

Using lsof Command

Another way to check for TCP connections is by using the lsof command. The lsof command is used to list open files, and it can be used to list all open TCP connections.

sudo lsof |grep -i ESTABLISHED

This command will show the number of established TCP connections.

Conclusion

In this blog post, we have discussed how to calculate TCP connections without using the ss and netstat commands. We have seen how to use the /proc/net/tcp file, the lsof command, and the ps command to check for TCP connections. These methods can be useful when the ss and netstat commands are not available or not permitted to use.

Leave a message