phpsocketsnetwork-programmingphp-socket

How to get a list of active network interfaces in php


So What I want is to achieve this through php standalone using socket functions-

$  sudo ifconfig | cut -d " " -f1 | awk 'NF==1{print $1}'
 eth0
 lo
 wlan0

I know I can do so by using system command but for that either I need to change sticky bit permission on ifconfig (root privelaged command) or need to write a C wrapper.

So please tell me the best possible way to achieve this through PHP.

My purpose is to display the available network interfaces list to the front end user using a application developed using HTML and PHP.


Solution

  • You can add the user your webserver runs as (usually, www or www-data) to /etc/sudoers. Then you should be able to access the ifconfig command via a system call. Obviously there is a security impact here. You should restrict the access to the ifconfig command if you take that route.

    $out = system("sudo ifconfig ...");