Jump to content

I have an array that I need to get into a db


Go to solution Solved by Jacques1,

Recommended Posts

I just for some reason cannot wrap my head around this.  I have this PHP code

$output = shell_exec("sudo arp-scan --interface=eth0 --localnet --retry 1 --timeout 50 --numeric --plain --quiet | grep -e 80:82:87");
print_r ($output)

Running this I get this on the page:

 

10.1.10.76 80:82:87:03:9d:38 10.1.10.84 80:82:87:03:d8:58 10.1.10.235 80:82:87:03:19:f0

 

This shows three devices on my network.  IP address and a MAC address.  I need to take that information and insert it into a db table.  

 

Can someone help me with how to accomplish this?  

 

Thanks

Based on your first response you are getting a string with 6 pieces of data separates by spaces. There are three pairs of data (IP and MAC).

 

Step 1: Split the data into an array of six elements

$deviceData = explode(' ', $output);

Step 2: Convert the array into separate elements for each device by 'chunking' it

$devices = array_chunk($deviceData, 2);

You should now have a multi-dimensional array with each sub-array representing a single device. Each sub-array would have two elements, the IP and the MAC address.

 

Step 3: Now you can loop over those elements and validate that the pieces of data are valid and insert/update into your DB

foreach($devices as $device)
{
    $deviceIP = $device[0];
    $deviceMAC = $device[1];
    //Validate the values and then (if valid) insert/update into DB
}
Edited by Psycho

Thanks Psycho for your help.  I think I uderstand what you have given me.  It doesn't quite work yet.  I get this error so far:

 

Notice: Undefined offset: 1 in /var/www/html/cqadmin/macfind.php on line 11

 

When I set a print_r $devices  I get this.  To me it looks like the chunking isn't figuring out how to split the $output.

 

Array ( [0] => Array ( [0] => 10.1.10.76 80:82:87:03:9d:38 10.1.10.84 80:82:87:03:d8:58 10.1.10.235 80:82:87:03:19:f0 ) )

 

Here is the code I used for the results I got

<?php

error_reporting(E_ALL);
        ini_set('display_errors', '1');
$output = shell_exec("sudo arp-scan --interface=eth0 --localnet --retry 1 --timeout 50 --numeric --plain --quiet | grep -e 80:82:87");
$deviceData = explode(' ', $output);
$devices = array_chunk($deviceData,1);
Print_r ($devices);
foreach($devices as $device)
{
    $deviceIP = $device[0];
    $deviceMAC = $device[1];
    //Validate the values and then (if valid) insert/update into DB
}

?>

Thanks

NO, the explode is not exploding as expected. It must be using something other than a space to separate the values. You will need to inspect the actual characters to know what to explode the data with. It might be a tab character ("\t") or some other white-space character.

 

Output the variable $output to the page and then inspect the HTML to see if you can ascertain the actual character used as the delimiter. Post the actual HTML source here. 

Have you tried preg_split() and the white-space character (\s) for the pattern? Here is an example found through the Google link provided earlier: 

$oparray = preg_split('/\s+/', trim($output));
Edited by cyberRobot
  • Solution

You don't need no regular expressions. Just look at the output of the command in the console and then split the string at the right characters. Surely you can recognize a newline and a tab.

 

In my case, that's

foreach (explode("\n", $arpOutput) as $host)
{
    list($ip, $mac) = explode("\t", $host);

    echo "IP address: $ip, MAC address: $mac<br>";
}
Edited by Jacques1

Getting closer !

 

Using : $deviceData = preg_split('/\s+/', trim($output));  Instead of explode

 
I am getting :
 
Array ( [0] => 10.1.10.76 [1] => 80:82:87:03:9d:38 [2] => 10.1.10.84 [3] => 80:82:87:03:d8:58 [4] => 10.1.10.235 [5] => 80:82:87:03:19:f0 )
 
when I use print_r ($devices);

Using : $deviceData = preg_split('/\s+/', trim($output));  Instead of explode

 

Just to clarify, that was to replace step 1 of Psycho's solution. You should be able to use array_chunk() next, etc.

https://forums.phpfreaks.com/topic/302791-i-have-an-array-that-i-need-to-get-into-a-db/?do=findComment&comment=1540606

 

 

 

You don't need no regular expressions. Just look at the output of the command in the console and then split the string at the right characters. Surely you can recognize a newline and a tab.

 

Would there ever be a case where it uses "\r\n" instead of just "\n"? I honestly don't know.  :shrug:

Success

The Notice I had was caused by this line:

list($ip, $mac) = explode("\t", $host);

 

Through some experimentation and googling I came up with this:

 

list($ip, $mac) = array_pad(explode("\t", $host),2,null);
 
The notice is a thing of the past.  I now get both the IP and the MAC addresses of the devices in the network.  Now to get it into the DB
 
Thank you everyone for all your help!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.