Get NIC with IP Configuration related information with PHP

Hi, this tutorial will help you to get some network related info (such as – Network Interface Controller, MAC Address, Default Gateway, etc) using PHP. We can also see this information using the “ipconfig /all” command. But in some situation if you need to get this information this tutorial will be very helpful.

First of all we will create an html table so that after get those data we can show in the table structure. Now write the below code:

ob_start(); // Turn on output buffering
system(‘ipconfig /all’); //Execute external program to display output
$com_result=ob_get_contents(); // Capture the output into a variable
ob_clean(); // Clean (erase) the output buffer

ob_start():

This ob_start()  function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

system():

The system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module.

ob_get_contents():

The ob_get_contents() gets the contents of the output buffer without clearing it.

ob_clean():

The ob_clean() function discards the contents of the output buffer.

 

Now we have all information in the “$com_result” related to “ipconfig /all” command. Lets show it as table format using the below code:

<table style=”border:1px solid #CCCCCC; background-color:#F0F0F0; font-family:verdana; font-size:12px; margin:0 auto;” cellpadding=”5″ cellspacing=”2″ width=”600px”>
<tr>
<td colspan=”2″ align =”center”>
<h3>NIC with IP Configuration related information</h3>
</td>
</tr>
<tr>
<td width=”250″><b>NIC:</b></td>
<td>
<?php
$desc = “Description”;
$desc_pos = strpos($com_result, $desc); // Find the position of Description text
$physical = “Physical”;
$physical_pos = strpos($com_result, $physical); // Find the position of Physical text
$nic_size = $physical_pos – ($desc_pos+36);

$desc=substr($com_result,($desc_pos+36),$nic_size); // Get Full line of Description Address
echo $desc;
?>
</td>
</tr>
<tr>
<td width=”200″><b>Physical Address (MAC Address):</b></td>
<td>
<?php
$findme = “Physical”;
$str_pos = strpos($com_result, $findme); // Find the position of Physical text
$mac=substr($com_result,($str_pos+36),17); // Get Full line of Physical Address
echo $mac;
?>
</td>
</tr>
<tr>
<td width=”200″><b>Is DHCP Enabled</b></td>
<td>
<?php
$findme = “Dhcp Enabled”;
$str_pos = strpos($com_result, $findme); // Find the position of “Dhcp Enabled” text
$dhcp_enabld=substr($com_result,($str_pos+36),3); // Get Full line of “Dhcp Enabled”
echo $dhcp_enabld;
?>
</td>
</tr>
<tr>
<td width=”200″><b>Is Autoconfiguration Enabled</b></td>
<td>
<?php
$findme = “Autoconfiguration Enabled”;
$str_pos = strpos($com_result, $findme); // Find the position of “Autoconfiguration Enabled” text
$autocon_enabld=substr($com_result,($str_pos+36),3); // Get Full line of “Autoconfiguration Enabled”
echo $autocon_enabld;
?>
</td>
</tr>
<tr>
<td width=”200″><b>IP Address</b></td>
<td>
<?php
$findme = “IP Addres”;
$str_pos = strpos($com_result, $findme); // Find the position of “IP Address” text
$ip_addres=substr($com_result,($str_pos+36),15); // Get Full line of “IP Address”
echo $ip_addres;
?>
</td>
</tr>
<tr>
<td width=”200″><b>Subnet Mask</b></td>
<td>
<?php
$findme = “Subnet”;
$str_pos = strpos($com_result, $findme); // Find the position of Subnet text
$subnet_mask=substr($com_result,($str_pos+36),15); // Get Full line of Subnet Mask
echo $subnet_mask;
?>
</td>
</tr>
<tr>
<td width=”200″><b>Default Gateway</b></td>
<td>
<?php
$findme = “Default Gateway”;
$str_pos = strpos($com_result, $findme); // Find the position of “Default Gateway” text
$Default_Gateway=substr($com_result,($str_pos+36),15); // Get Full line of Default Gateway
echo $Default_Gateway;
?>
</td>
</tr>
<tr>
<td width=”200″><b>DHCP Server</b></td>
<td>
<?php
$findme = “DHCP Server”;
$str_pos = strpos($com_result, $findme); // Find the position of “DHCP Server” text
$dhcp_server=substr($com_result,($str_pos+36),15); // Get Full line of “DHCP Server”
echo $dhcp_server;
?>
</td>
</tr>
<tr>
<td width=”200″><b>DNS Server</b></td>
<td>
<?php
$findme = “DNS Servers”;
$str_pos = strpos($com_result, $findme); // Find the position of “DNS Servers” text
$dns_server=substr($com_result,($str_pos+36),15); // Get Full line of “DNS Servers”
echo $dns_server;
?>
</td>
</tr>
</table>

Wish the output is as the below screen shot:

You can download the script by clicking here. Let me know if you find any difficulties with this tutorial. Enjoy!!!

Author Info

Shah Alom

Hi, This is Mohammad Shah Alom, My passion is Programming & Web Development. I am Founder of Micro Solutions Bangladesh. My Facebook profile shahalom1983 & Twitter Profile shahalom_83

  • diablo 3 release date official

    I would like to thnkx with the efforts you’ve place in producing this weblog. I’m hoping the same high-grade weblog publish from you in the upcoming as well. In fact your creative creating abilities has inspired me to acquire my own website now. Actually the running a blog is spreading its wings quickly. Your write up is really a very good example of it.

    • I told my kids we’d play after I found what I neeedd. Damnit.