use*_*744 5 linux ssh netstat ps
我基本上尝试编写一个类似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命令.
linux 2.6.18
只有openSSH的"远程"东西.目前正在运行OpenSSH_4.3p2.
当然,SSH访问所有节点(基于密钥的身份验证),因此可以从所有节点获得ps和netstat.
仅Linux的"黑客"很好,不需要便携,但当然这将是一个额外的好处.
用户将始终保持不变,并且我的命令/脚本正在以该用户身份运行.该用户不是root用户.
不必快,只准确.
自发的解决方案是编写一个pstree克隆,在命令字符串" ssh" 上触发,找出源端口然后转到相关的远程机器并找出sshd由这个特定命令生成的孩子中的哪一个.
但也许有一种更聪明的方式呢?:P
实际上,我认为您自发的解决方案是正确的方法:使用 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
我很想看看结果,因为它对我非常有用!