Damiano
Damiano

Reputation: 1

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 use this script on Windows.

Thank you!

Upvotes: 0

Views: 685

Answers (2)

initzero
initzero

Reputation: 852

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

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318518

You could parse the output of /sbin/ip addr

Upvotes: 1

Related Questions