Question

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.

Was it helpful?

Solution 2

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 ...");

OTHER TIPS

on a Debian based distributions, You can use this library that I wrote. it can read/write /etc/network/interfaces

https://github.com/carp3/networkinterfaces

<?php
//include composer autoloader
include 'vendor/autoload.php';

// 'import' NetworkInterfaces class
use NetworkInterfaces\Adaptor;
use NetworkInterfaces\NetworkInterfaces;

// create new handle from /etc/networking/interfaces
$handle = new NetworkInterfaces('/etc/networking/interfaces');

// parse file
$handle->parse();

// print all adaptors
var_dump($handle->Adaptors);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top