如何在不使用 lsof 或 netstat 的情况下将网络连接与 PID 绑定?

发布于 2024-07-18 08:54:31 字数 1562 浏览 5 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌无敌 2024-07-25 08:54:31

我不知道你需要多久轮询一次,也不知道你所说的“昂贵”是什么意思,但是使用正确的选项,netstatlsof的运行速度比默认配置。
示例:

netstat -ltn

仅显示监听tcp 套接字,并忽略默认启用的(慢速)名称名称分辨率。

lsof -b -n -i4tcp:80

省略所有b锁定操作、名称名称解析,并将选择限制为端口 80 上的 IPv4 tcp 套接字。

I don't know how often you need to poll, or what you mean with "expensive", but with the right options both netstat and lsof run a lot faster than in the default configuration.
Examples:

netstat -ltn

shows only listening tcp sockets, and omits the (slow) name resolution that is on by default.

lsof -b -n -i4tcp:80

omits all blocking operations, name resolution, and limits the selection to IPv4 tcp sockets on port 80.

丑丑阿 2024-07-25 08:54:31

在 Solaris 上,您可以使用 pfiles(1) 来执行此操作:

# ps -fp 308 
     UID   PID  PPID   C    STIME TTY         TIME CMD
    root   308   255   0 22:44:07 ?           0:00 /usr/lib/ssh/sshd
# pfiles 308 | egrep 'S_IFSOCK|sockname: '
   6: S_IFSOCK mode:0666 dev:326,0 ino:3255 uid:0 gid:0 size:0
        sockname: AF_INET 192.168.1.30  port: 22

对于 Linux,这更复杂(可怕):

# pgrep sshd
3155
# ls -l /proc/3155/fd | fgrep socket
lrwx------ 1 root root 64 May 22 23:04 3 -> socket:[7529]
# fgrep 7529 /proc/3155/net/tcp 
   6: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7529 1 f5baa8a0 300 0 0 2 -1            

00000000:00160.0.0.0:22 。 以下是 netstat -a 的等效输出:

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN

On Solaris you can use pfiles(1) to do this:

# ps -fp 308 
     UID   PID  PPID   C    STIME TTY         TIME CMD
    root   308   255   0 22:44:07 ?           0:00 /usr/lib/ssh/sshd
# pfiles 308 | egrep 'S_IFSOCK|sockname: '
   6: S_IFSOCK mode:0666 dev:326,0 ino:3255 uid:0 gid:0 size:0
        sockname: AF_INET 192.168.1.30  port: 22

For Linux, this is more complex (gruesome):

# pgrep sshd
3155
# ls -l /proc/3155/fd | fgrep socket
lrwx------ 1 root root 64 May 22 23:04 3 -> socket:[7529]
# fgrep 7529 /proc/3155/net/tcp 
   6: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7529 1 f5baa8a0 300 0 0 2 -1            

00000000:0016 is 0.0.0.0:22. Here's the equivalent output from netstat -a:

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
薄荷梦 2024-07-25 08:54:31

对于 Linux,请查看 /proc/net 目录
(例如,cat /proc/net/tcp 列出您的 tcp 连接)。 不确定 Solaris 的情况。

更多信息请参见此处

我想 netstat 基本上使用完全相同的信息,所以我不知道你是否能够加快它的速度。 请务必尝试使用 netstat '-an' 标志来不将 ip 地址实时解析为主机名(因为由于 dns 查询,这可能会花费大量时间)。

For Linux, have a look at the /proc/net directory
(for example, cat /proc/net/tcp lists your tcp connections). Not sure about Solaris.

Some more information here.

I guess netstat basically uses this exact same information so i don't know if you will be able to speed it up a whole lot. Be sure to try the netstat '-an' flags to NOT resolve ip-adresses to hostnames realtime (as this can take a lot of time due to dns queries).

下壹個目標 2024-07-25 08:54:31

最简单的事情是

strace -f netstat -na

在 Linux 上(我不知道 Solaris 上的情况)。 这将为您提供所有系统调用的日志。 这是很多输出,其中一些是相关的。 查看它正在打开的 /proc 文件系统中的文件。 这应该会引导您了解 netstat 是如何做到这一点的。 不雅的是,ltrace 将允许您通过 c 库做同样的事情。 在这种情况下对您没有用,但在其他情况下可能很有用。

如果还不清楚,请查看来源。

The easiest thing to do is

strace -f netstat -na

On Linux (I don't know about Solaris). This will give you a log of all of the system calls made. It's a lot of output, some of which will be relevant. Take a look at the files in the /proc file system that it's opening. This should lead you to how netstat does it. Indecently, ltrace will allow you to do the same thing through the c library. Not useful for you in this instance, but it can be useful in other circumstances.

If it's not clear from that, then take a look at the source.

白色秋天 2024-07-25 08:54:31

netstat 或 lsof 的替代方案是 fusionr

$ fusionr 22/tcp
22/tcp: 547 825 842 896 898

或者如果您想知道它是如何工作的(对于 ipv4 tcp)并自行操作:

$ ls -l /proc//fd/ | grep $(PORT=22 grep printf ":%04x" $PORT /proc/net/tcp | awk '{printf(" -e %s",$10); }') | awk -F/ '{打印 $3 }'

alternative for netstat or lsof is fuser

$ fuser 22/tcp
22/tcp: 547 825 842 896 898

or if you like to know how it works (for ipv4 tcp) and do it on your own:

$ ls -l /proc//fd/ | grep $(PORT=22 grep printf ":%04x" $PORT /proc/net/tcp | awk '{printf(" -e %s",$10); }') | awk -F/ '{print $3 }'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文