跨机器跟踪 pid (ssh)

发布于 2024-08-21 00:56:16 字数 943 浏览 8 评论 0原文

我基本上是在尝试编写一个类似 pstree 的命令,只不过它应该遵循跨机器的进程。

我的意思是,如果我运行这个:

$ ssh $node sleep 1000

那么命令应该显示类似这样的内容:

ssh $node -- ($node) sleep 1000

如果我正在运行:

$ ssh $node ssh $node sleep 1000

ssh $node---($node) ssh $node---($node) sleep 1000

等等...

我的问题是:如何将一台机器上的一个 ssh 会话映射到生成的机器上在另一台机器上处理?

本地父子进程不是问题,但如何找出一个节点上的哪个 ssh 命令触发了另一个节点上的另一个进程。

  1. linux 2.6.18

  2. 仅 openSSH 用于“远程”功能。当前正在运行 OpenSSH_4.3p2。

  3. 当然可以通过 SSH 访问所有节点(基于密钥的身份验证),因此所有节点都可以使用 ps 和 netstat。

  4. 仅限 Linux 的“黑客”很好,不需要可移植,尽管这当然会是一个额外的好处。

    仅限 Linux 的“黑客”很好,不需要可移植,尽管这会是一个额外的好处
  5. 用户将始终相同,并且我的命令/脚本以该用户身份运行。该用户不是 root。

  6. 不一定要快,只要准确。

自发的解决方案是编写一个 pstree 克隆,它在命令字符串“ssh”上触发,找出源端口,然后转到有问题的远程计算机并找出 sshd 的哪个子级是由该特定命令生成的。

但也许有更聪明的方法? :P

I am basically trying to write a pstree-like command except that it should follow processes across machines.

What I mean is that if I run this :

$ ssh $node sleep 1000

Then the command should display something like this :

ssh $node -- ($node) sleep 1000

And if I'm running :

$ ssh $node ssh $node sleep 1000

ssh $node---($node) ssh $node---($node) sleep 1000

And so on ...

My question is this : How can I map one ssh session on one machine to a spawned process on another machine ?

Local parent-child processes are not a problem, but how can I figure out which ssh command on one node that triggered another process on another node.

  1. linux 2.6.18

  2. only openSSH for "remote" stuff. Running OpenSSH_4.3p2 currently.

  3. SSH access to all nodes of course (key based auth) so ps and netstat are available from all nodes.

  4. Linux-only "hacks" are fine, does not need to be portable though that would be an added bonus of course.

  5. The user will always be the same and my command/script is running as that user. That user is not root.

  6. Does not have to be fast, only accurate.

The spontaneous solution would be to write a pstree clone, that triggers on the command string "ssh", figures out the source-port and then goes to the remote machine in question and figures out which one of sshd's children that was spawned by this particular command.

But maybe there's a more clever way ? :P

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

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

发布评论

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

评论(1

诗笺 2024-08-28 00:56:16

实际上,我认为您自发的解决方案是正确的方法:使用 netstat 获取源端口并在远程计算机上查找它。如果不是 root,您可能会遇到使用“netstat -p”的问题 - 我在两台机器上尝试过,其中一台很乐意向我展示我自己的进程,而另一台则不然。

除了 ssh 客户端之外,您还可以扩展它来查找使用 ssh 连接的其他客户端,例如 rsync 或 Mercurial。只是要小心,不要递归地跟踪程序自己的连接!

使用 netstat 和 pstree 进行的快速实验表明这个想法是合理的:

me@mymachine:~$ netstat -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 mymachine.example:43681 remote.example.com:ssh ESTABLISHED 27044/ssh
tcp        0      0 mymachine.example:39228 remote.example.com:ssh ESTABLISHED 14499/ssh
tcp        0      0 mymachine.example:45814 remote.example.com:ssh ESTABLISHED 20899/ssh
 me@mymachine:~$ ssh remote netstat -p | grep mymachine.example:43681
tcp        0      0 remote.example.com:ssh mymachine.example:43681 ESTABLISHED 10361/1
me@mymachine:~$ ssh remote pstree -a 10361
sshd
  `-grep -n -e wotsit -i -R /local/home/me/somewhere /dev/null

我有兴趣查看结果,因为它对我非常有用!

Actually, I think your spontaneous solution is the right way to do it: use netstat to get the source-port and look for it on the remote machine. You might have trouble using "netstat -p" without being root - I tried it on two machines, one which was happy to show me my own processes and one which wasn't.

As well as ssh clients, you might extend this to look for other clients that use ssh connections, like rsync or Mercurial. Just be careful not to trace your program's own connection recursively!

A quick experiment with netstat and pstree shows that the idea is sound:

me@mymachine:~$ netstat -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 mymachine.example:43681 remote.example.com:ssh ESTABLISHED 27044/ssh
tcp        0      0 mymachine.example:39228 remote.example.com:ssh ESTABLISHED 14499/ssh
tcp        0      0 mymachine.example:45814 remote.example.com:ssh ESTABLISHED 20899/ssh
 me@mymachine:~$ ssh remote netstat -p | grep mymachine.example:43681
tcp        0      0 remote.example.com:ssh mymachine.example:43681 ESTABLISHED 10361/1
me@mymachine:~$ ssh remote pstree -a 10361
sshd
  `-grep -n -e wotsit -i -R /local/home/me/somewhere /dev/null

I'd be interested to see the result, because it would be very useful to me!

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