Как получить список открытых сокетов в Linux с помощью C?

Еще один способ сделать это - преобразовать XML в json:

import xmltodict

with open('file.xml') as f:
    d = xmltodict.parse(f.read())['fruits']['tag']

for i in d['item']:
    subtag = []
    for s in i['subtag']['Info']:
        subtag.append('{};{}'.format(s['@name'], s['#text']))
    print('{}|{}|{}|{}|{}|{}|{}|'.format(d['@beginTime'], d['@endTime'], d['EventId'], i['@color'], i['name'], i['count'], ';'.join(subtag)))

Вывод:

20181125020000|20181202020000|16778|red|apple|1|Eid;396;New;397|
20181125020000|20181202020000|16778|yellow|banana|2|Eid;500;New;650;Col;999|
7
задан Misha M 8 May 2009 в 20:45
поделиться

4 ответа

Open and read the following:

/proc/net/tcp - a list of open TCP sockets

/proc/net/udp - a list of open UDP sockets

/proc/net/raw - a list all the "raw" sockets

These are like "regular" files that you open and read with a filehandle and will give you all the information you could possibly need about each socket.

16
ответ дан 6 December 2019 в 07:52
поделиться

This program may be useful for you and demonstrates how to parse the /net/proc/* files sockstat.c

4
ответ дан 6 December 2019 в 07:52
поделиться

In directory /proc/self/fd there are fake symlinks giving you all your open file descriptors - sockets give something like:

lrwx------ 1 root root 64 2009-05-08 07:45 4 -> socket:[4921]
lrwx------ 1 root root 64 2009-05-08 07:45 5 -> socket:[4918]
lrwx------ 1 root root 64 2009-05-08 07:45 6 -> socket:[5395]

Iterate them using opendir, readdir() and then interrogate them using readlink()

If you know that FD 4 is a socket, you can then call getsockname() on it to get the local address family, address etc, if bound.

3
ответ дан 6 December 2019 в 07:52
поделиться

The raw data can be found at /proc/net/tcp, /proc/net/udp, etc. Refer to the header at the first line for a (terse) description.

0
ответ дан 6 December 2019 в 07:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: