Display socket options

2019-04-05 15:52发布

How I can see from shell what socket options are set? In particular I'm interesting to know if SO_BROADCAST is set?

标签: linux shell
2条回答
做个烂人
2楼-- · 2019-04-05 16:13

You can use lsof(8). If PID is the process ID and FD is the file descriptor number of the socket you're interested in, you can do this:

lsof -a -p PID -d FD -T f

To list all IPv4 sockets of a process:

lsof -a -p PID -i 4 -T f

This will print out the socket options with a SO=, among other information. Note that if no options are set, you'll get the empty string, so you'll see something like SO=PQLEN=0 etc. To test for SO_BROADCAST, just grep for the string SO_BROADCAST after the SO=, e.g.

if lsof -a -p PID -d FD -T f | grep -q 'SO=[^=]*SO_BROADCAST'; then
    # socket has SO_BROADCAST
else
    # it doesn't
fi
查看更多
Viruses.
3楼-- · 2019-04-05 16:14

On Linux, you can use the knetstat kernel module to inspect socket options, including SO_BROADCAST.

查看更多
登录 后发表回答