我想知道机器的所有已配置的IP地址,我可以用PHP获取此信息吗?我正在使用linux,不想在Windows上使用这个脚本。
谢谢!
答案 0 :(得分:1)
您可以解析/sbin/ip addr
答案 1 :(得分:0)
编辑:添加了/ sbin / ip addr示例。这是一种解析它的简单方法。
ip addr |grep "inet" |grep -v "inet6" |cut -d"/" -f1 |cut -d" " -f6
假设您的输出看起来像我的,这应该可以正常工作。如果你的不同,它也很容易调整。
ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1
对于ifconfig输出,如下所示:
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: ..... Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:943395 errors:0 dropped:0 overruns:0 frame:0
TX packets:173679 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:191981114 (191.9 MB) TX bytes:32206803 (32.2 MB)
Interrupt:16
eth1 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet6 addr: ...... Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:9969 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:3192069 (3.1 MB)
Interrupt:17 Base address:0xe000 Memory:dfcff000-dfcfffff
eth1:avahi Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:17 Base address:0xe000 Memory:dfcff000-dfcfffff
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:52889 errors:0 dropped:0 overruns:0 frame:0
TX packets:52889 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:18763697 (18.7 MB) TX bytes:18763697 (18.7 MB)
返回以下内容:
192.168.0.1
192.168.1.1
127.0.0.1