开发者

How to get all the configured IPs of the machine?

I would like to know all the configured IPs addresses of the machine, can I get this information with PHP? I'm using linux, do not want to 开发者_StackOverflowuse this script on Windows.

Thank you!


You could parse the output of /sbin/ip addr


Edit: added /sbin/ip addr example. Here is an easy way to parse it.

ip addr |grep "inet" |grep -v "inet6" |cut -d"/" -f1 |cut -d" " -f6

  1. Call ip addr
  2. grep for lines "inet", excluding "inet6"
  3. split the result on "/", this results in " inet XXX.XXX.XXX.XXX"
  4. split the result on " ", take the 6th element, which should be the IP address.

Assuming your output looks something like mine, this should work fine. It is also pretty easy to tune if yours differs.

ifconfig |grep "inet addr" |cut -d: -f2 |cut -d" " -f1

For ifconfig output like the following:

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)

Returns the following:

192.168.0.1
192.168.1.1
127.0.0.1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜