从PID中查找IP

发布于 2024-12-16 01:51:14 字数 302 浏览 2 评论 0原文

我是 shell 编程的新手,我想从进程 ID 中找到 IP 地址。现在,我可以从以下位置获取特定进程的 PID:

vmname=$1
pid=`ps aux | grep $vmname | awk 'NR==1{printf("%s\n", $2) }'`
echo $pid

上述方法返回 PID,但如何从 pid 获取端口?如果我获取了端口,是否也有命令可以获取 IP 地址? 我使用的是 Ubuntu 11.04,上面的脚本实际上是试图使用这种方法找出在 KVM 上运行的虚拟机的 IP。

谢谢!

I'm a newbie to shell programming and I'd like to find the IP address from the process ID. Right now, I'm able to get the PID for a specific process from :

vmname=$1
pid=`ps aux | grep $vmname | awk 'NR==1{printf("%s\n", $2) }'`
echo $pid

The above method returns the PID but how do I get the port from the pid? If I get the port, is there a command to get the IP address as well?
I'm using Ubuntu 11.04 and the above script is actually trying to find out the IP of a virtual machine running on KVM using this method.

Thanks!

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

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

发布评论

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

评论(3

雪若未夕 2024-12-23 01:51:14

您可以使用lsof实用程序。它给出了进程的打开文件列表。使用lsof -p pid。您需要对输出进行 grep 来获取例如的端口值。像这样的东西 - lsof -p pid| grep TCP。这将列出该进程打开或连接的所有端口。请参阅该实用程序的手册。对于大多数系统,该实用程序预先与您的操作系统捆绑在一起。但是,如果它没有预先捆绑,那么您需要安装此实用程序。

You can employ the lsof utility. It gives the list of open files for a process. Use lsof -p pid . You need to grep on the output to get the port values for eg. something like this - lsof -p pid| grep TCP. This will list all the ports opened or connected to by the process. Refer to the manual of the utility. For most systems the utility comes pre-bundled with your OS. However, if it is not pre-bundled then you need to install this utility.

他不在意 2024-12-23 01:51:14

PID和计算机的IP地址是两个完全不相关的东西。

PID 代表进程 ID,它是操作系统跟踪程序等的句柄。

IP地址与网络接口相关。大多数计算机都有其中一两个(在以太网卡/无线设备的情况下)。

无论如何,获取计算机 IP 地址的一种方法类似于以下内容......很可能有更好的方法来做到这一点,我只是不知道他们......

$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 60:eb:69:96:da:87  
          inet addr:192.168.1.112  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::62eb:69ff:fe96:da87/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:876533 errors:0 dropped:0 overruns:0 frame:0
          TX packets:560999 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:229205080 (229.2 MB)  TX bytes:136756800 (136.7 MB)
          Interrupt:40 Base address:0x8000 

$ ifconfig eth0 | grep "inet addr"
          inet addr:192.168.1.112  Bcast:192.168.1.255  Mask:255.255.255.0
$ ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2
192.168.1.112  Bcast
$ ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1
192.168.1.112

所以最后一个命令将为您提供脚本中想要的内容。其余的只是向您展示我是如何构建到最后一个命令的。

The PID and the computer's IP Address are two completely unrelated things.

PID stands for Process ID, and it's a handle for the OS to keep track of your program, among other things.

IP address is related to a network interface. Most computers have one or two of these (in the case of ethernet card/wireless device.)

Anyway, one way to get your computer's IP address is something similar to the following...There are quite possibly better ways to do it and I just don't know 'em...

$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 60:eb:69:96:da:87  
          inet addr:192.168.1.112  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::62eb:69ff:fe96:da87/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:876533 errors:0 dropped:0 overruns:0 frame:0
          TX packets:560999 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:229205080 (229.2 MB)  TX bytes:136756800 (136.7 MB)
          Interrupt:40 Base address:0x8000 

$ ifconfig eth0 | grep "inet addr"
          inet addr:192.168.1.112  Bcast:192.168.1.255  Mask:255.255.255.0
$ ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2
192.168.1.112  Bcast
$ ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1
192.168.1.112

So the last command will get you what you want inside your script. The rest are just there to show you how I built up to the last command.

故事和酒 2024-12-23 01:51:14

在开始之前,如果 lsof 且 root/su/sudo 访问权限可用,则应按照@Drona 的建议使用 lsof。

为了完整性,我正在调查此问题,以获取当前登录的 chroot SFTP 用户的 IP 地址,以获取 nagios 脚本,但我不想为其创建 sudoers 规则。

简单的方法(不像lsof那么简单,需要 root,但为了完整性)

步骤 1

$ ps -ef | grep ssh
UID        PID  PPID  C STIME TTY          TIME CMD
root      2479 14186  0 17:05 ?        00:00:00 sshd: sftpuser [priv]
1008      2481  2479  0 17:06 ?        00:00:00 sshd: sftpuser@notty
1008      2482  2481  0 17:06 ?        00:00:00 sshd: sftpuser@internal-sftp
root      2483 14186  0 17:06 ?        00:00:00 sshd: ttyuser [priv]
ttyuser  2485  2483  0 17:06 ?        00:00:00 sshd: ttyuser@pts/0

在上面你可以看到 ssh 用户的 PID(添加了 ps 列以便于解释)

步骤 2

sudo lsof -p 2481 | grep TCP
sshd    2481 root    3u  IPv4           29176657      0t0      TCP 192.168.1.2:44156 (ESTABLISHED)

替代方案(更复杂的是不需要 rood 的可能性)

步骤 2 - 需要 root 访问权限,但可选

$ sudo ls -l  /proc/2481/fd
total 0
lrwx------ 1 root root 64 Jul  3 17:07 0 -> /dev/null
lrwx------ 1 root root 64 Jul  3 17:07 1 -> /dev/null
lr-x------ 1 root root 64 Jul  3 17:07 11 -> pipe:[29209918]
lrwx------ 1 root root 64 Jul  3 17:07 2 -> /dev/null
lrwx------ 1 root root 64 Jul  3 17:07 3 -> socket:[29209894]
lrwx------ 1 root root 64 Jul  3 17:07 5 -> socket:[29211080]
lr-x------ 1 root root 64 Jul  3 17:07 6 -> pipe:[29209915]
l-wx------ 1 root root 64 Jul  3 17:07 7 -> pipe:[29209915]
l-wx------ 1 root root 64 Jul  3 17:07 8 -> pipe:[29209916]
lr-x------ 1 root root 64 Jul  3 17:07 9 -> pipe:[29209917]

步骤 3

$ fgrep 29209894 /proc/2481/net/tcp 
   8: 0101A8C0:0016 0201A8C0:B0B0 ...

这里 fgrep 使用 socked 上的号码和 PID 来提取信息。

重要信息是0101A8C0:00160201A8C0:B0B0。第一个与服务器相关,第二个与连接的客户端相关,其中第一部分(用冒号分隔)是反向 IP 地址的十六进制表示形式,第二部分是端口的十六进制表示形式。 IE
<代码> 0101A8C0 - > 1.1.168.192-> 192.168.1.1。如果您知道服务器正在侦听的端口,则可以跳过步骤 2 并使用以下命令代替步骤 3

步骤 2 + 3 知道服务器端口后进行替换 - 如果

在这种情况下没有可用的 root,因为我正在检查标准 ssh 端口 22(十六进制 0016)上的 SFTP 连接

$ fgrep 0016 /proc/2481/net/tcp 
   8: 0101A8C0:0016 0201A8C0:B0B0 ...

Before I start lsof should be used as suggested by @Drona if lsof and if root/su/sudo access is available.

For completness I was investigating this for getting the IP address of currently logged in chrooted SFTP users for a nagios script I did not want to have to create a sudoers rule for.

Easy way (not as easy as lsof and needs root but for completeness)

Step 1

$ ps -ef | grep ssh
UID        PID  PPID  C STIME TTY          TIME CMD
root      2479 14186  0 17:05 ?        00:00:00 sshd: sftpuser [priv]
1008      2481  2479  0 17:06 ?        00:00:00 sshd: sftpuser@notty
1008      2482  2481  0 17:06 ?        00:00:00 sshd: sftpuser@internal-sftp
root      2483 14186  0 17:06 ?        00:00:00 sshd: ttyuser [priv]
ttyuser  2485  2483  0 17:06 ?        00:00:00 sshd: ttyuser@pts/0

Above you can see the PID for the ssh users (added the ps columns for easier interpretation)

Step 2

sudo lsof -p 2481 | grep TCP
sshd    2481 root    3u  IPv4           29176657      0t0      TCP 192.168.1.2:44156 (ESTABLISHED)

Alternative (more complex has the possibility of not needing rood)

Step 2 - Requires root access but is optional

$ sudo ls -l  /proc/2481/fd
total 0
lrwx------ 1 root root 64 Jul  3 17:07 0 -> /dev/null
lrwx------ 1 root root 64 Jul  3 17:07 1 -> /dev/null
lr-x------ 1 root root 64 Jul  3 17:07 11 -> pipe:[29209918]
lrwx------ 1 root root 64 Jul  3 17:07 2 -> /dev/null
lrwx------ 1 root root 64 Jul  3 17:07 3 -> socket:[29209894]
lrwx------ 1 root root 64 Jul  3 17:07 5 -> socket:[29211080]
lr-x------ 1 root root 64 Jul  3 17:07 6 -> pipe:[29209915]
l-wx------ 1 root root 64 Jul  3 17:07 7 -> pipe:[29209915]
l-wx------ 1 root root 64 Jul  3 17:07 8 -> pipe:[29209916]
lr-x------ 1 root root 64 Jul  3 17:07 9 -> pipe:[29209917]

Step 3

$ fgrep 29209894 /proc/2481/net/tcp 
   8: 0101A8C0:0016 0201A8C0:B0B0 ...

here fgrep uses the number on the socked and the PID to extract the information.

The important information is 0101A8C0:0016 and 0201A8C0:B0B0. The first relates to ther server and the second is the connected client where the first part (split by the colon) is the hexadecimal representation of the reversed IP address and the second is the hexadecimal representation of the port. i.e
0101A8C0 -> 1.1.168.192 -> 192.168.1.1. If you know the port the server is listening on you can skip Step 2 and use the following instead of Step 3.

Step 2 + 3 Replacement when knowing the server port - if no root is availalble

in this case as I was checking for SFTP connections on the standard ssh port of 22 (in hex 0016)

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