Display socket options

后端 未结 3 1161
不思量自难忘°
不思量自难忘° 2021-02-20 02:24

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

3条回答
  •  渐次进展
    2021-02-20 02:57

    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
    

提交回复
热议问题