3

Is there an interface to get the currently connected IPs and their state in Linux? I am aware of ss and netstat, but I want to use /proc/ or some other "official" kernel interface that already has them (if it exists). If it doesn't exist where would I start to get this information? Basically, I need an interface that has this information so I can retrieve it programmatically.

4

Besides ss and netstat I currently don't know any other tools that I'd recommend.

For how to get this information:

If you man netstat, you can see at the section FILES some listed files that netstat uses to collect its information.

Among these, there are /proc/net/tcp and /proc/net/udp.

If you cat /proc/net/tcp you can see various information about tcp connections on your system.

A sample output would be

sl local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
0: 0101007F:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 11190 1 0000000000000 000 100 0 0 10 0
1: 8700A8C0:91FC 0F02000A:15B3 01 00000000:00000000 02:00000AF6 00000000  1000        0 5565254 2 00000000000 00000 46 4 13 10 -1

rem_address here is the IP you are looking for. I don't know much about it, but I think st gives you information about the current state. 0A should mean LISTEN, 01 means ESTABLISHED.

Decoding a local_address or rem_address is rather easy, 8700A8C0:8F76 for example:

Format: hex(rev_ip):hex(port)

87 -> 135
00 -> 0
A8 -> 168
C0 -> 192
:8F76 -> 36726

=> 192.168.0.135, Port 36726

More information about the /proc/net directory is here.

More information about the presented data is here.

A related SO thread is also here.

1

If you run strace lsof -i 2>&1 | grep open you'll get some clues about how lsof -i works:

open("/proc/1/fdinfo/7", O_RDONLY)      = 5
open("/proc/net/ax25", O_RDONLY)        = -1 ENOENT (No such file or directory)
open("/proc/net/ipx", O_RDONLY)         = -1 ENOENT (No such file or directory)
open("/proc/net/raw", O_RDONLY)         = 5
open("/proc/net/netlink", O_RDONLY)     = 5
open("/proc/net/packet", O_RDONLY)      = 5
open("/proc/net/unix", O_RDONLY)        = 5

Notice a few of the files do not exist on my system. Some of those files are documented in man proc, but their output does not look too hard to understand.

After that, lsof -i literally goes through the entire process table, in ascending order, looking for open descriptors:

open("/proc/2/stat", O_RDONLY)          = 4
openat(AT_FDCWD, "/proc/2/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/3/stat", O_RDONLY)          = 4
openat(AT_FDCWD, "/proc/3/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/5/stat", O_RDONLY)          = 4
openat(AT_FDCWD, "/proc/5/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/7/stat", O_RDONLY)          = 4
openat(AT_FDCWD, "/proc/7/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/8/stat", O_RDONLY)          = 4
[...]
openat(AT_FDCWD, "/proc/101/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/112/stat", O_RDONLY)        = 4
openat(AT_FDCWD, "/proc/112/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/143/stat", O_RDONLY)        = 4
openat(AT_FDCWD, "/proc/143/fd", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
open("/proc/143/fdinfo/3", O_RDONLY)    = 5
open("/proc/143/fdinfo/4", O_RDONLY)    = 5
open("/proc/143/fdinfo/5", O_RDONLY)    = 5
open("/proc/143/fdinfo/11", O_RDONLY)   = 5
open("/proc/143/fdinfo/12", O_RDONLY)   = 5
open("/proc/143/fdinfo/13", O_RDONLY)   = 5
1

An alternative, if you are using iptables and have ip_conntrack compiled into your kernel or loaded as a module, then you can get the iptables view of all connection state from /proc/net/ip_conntrack or the newer userspace interface tool conntrack

$ sudo cat /proc/net/ip_conntrack
icmp     1 23 src=10.1.1.14 dst=10.1.1.1 type=8 code=0 id=10017 src=10.1.1.1 dst=10.1.1.14 type=0 code=0 id=10017 mark=0 use=2
unknown  2 597 src=10.1.1.10 dst=224.0.0.1 [UNREPLIED] src=224.0.0.1 dst=10.1.1.10 mark=0 use=2
udp      17 17 src=10.1.1.181 dst=10.1.1.255 sport=17500 dport=17500 [UNREPLIED] src=10.1.1.255 dst=10.1.1.181 sport=17500 dport=17500 mark=0 use=2
tcp      6 431999 ESTABLISHED src=10.1.1.14 dst=10.1.1.2 sport=22 dport=49218 src=10.1.1.2 dst=10.1.1.14 sport=49218 dport=22 [ASSURED] mark=0 use=2
icmp     1 28 src=10.1.1.14 dst=8.8.8.8 type=8 code=0 id=13601 src=8.8.8.8 dst=10.1.1.14 type=0 code=0 id=13601 mark=0 use=2

This will also include information for connections that are routed through your machine, not just local connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.