需要使用PHP在Ubuntu上找到网络接口的IP地址 [英] Need to find IP address of network interface on Ubuntu with PHP

查看:32
本文介绍了需要使用PHP在Ubuntu上找到网络接口的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助查找计算机在网络上时的IP地址.我正在构建一个信息亭类型的系统,该系统将放置在不同的位置,并且需要能够使用Web浏览器在本地网络上找到该计算机的IP地址.

I need help finding the IP address of my computer when it is on a network. I am building a kiosk type system that will be put into different locations and I need to be able to use the web browser to find the IP address of that computer on the local network.

如果我使用 $ _ SERVER ['SERVER_ADDR'] ,我将获得通过该计算机上的本地浏览器(127.0.0.1)连接的IP地址.

If I use $_SERVER['SERVER_ADDR'] I get the IP address I am connecting to through the local browser (127.0.0.1) on that machine.

我无法喊出并获取公共IP,因为这些设备可能位于路由器后面,并且我不希望路由器的公共IP地址.

I can't call out and get the public IP because the units may be behind a router and I don't want the public IP address of the router.

我需要在服务器上找到该盒子的IP地址(例如:192.168.0.xxx)

I need to find the IP address of that box on the server (ex: 192.168.0.xxx)

我确实知道,当我从终端执行"ip addr show"时,我会得到

I do know that when I do a 'ip addr show' from the terminal I get

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 61:a6:4d:63:a2:80 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.211/24 brd 192.168.0.255 scope global em1
    inet6 fe80::62a4:4cff:fe64:a399/64 scope link 
       valid_lft forever preferred_lft forever

当我尝试时:

$command="ip addr show";
$localIP = exec ($command);

$ localIP 带有"valid_lft forever preferred-lever forever",但没有其他信息.如果我可以将所有内容都放入 $ localIP 中,那么我可以过滤出inet IP地址,但不能提供所有功能.

$localIP comes out with "valid_lft forever preferred_lft forever" but none of the other information. If I can get the whole thing into $localIP then I can filter out the inet IP address but it won't give me everything.

是否有更简单的方法来执行此操作,或者在尝试执行"ip addr show"命令时缺少了某些内容?另外,我正在用户apache下运行,并且无法以root身份运行此应用程序.

Is there an easier way to do this or something I am missing when trying to do the "ip addr show" command? Also, I am running under the user apache and can't run this as root for this application.

推荐答案

exec() ,该函数仅返回exec'd命令输出的最后一行.要捕获所有输出,您必须对函数使用可选的2nd参数:

As documented for exec(), only the LAST line of output from the exec'd command is returned from the function. To capture all of the output, you have to use the optional 2nd argument to the function:

$last_line = exec('ip addr show', $full_output);
                                  ^^^^^^^^^^^^

$ full_output 将是执行程序的输出行数组.

$full_output will be an array of lines of output from the exec'd program.

这篇关于需要使用PHP在Ubuntu上找到网络接口的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆