Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I run an arp-scan and put the results in a mysql table

Status
Not open for further replies.

EdLentz

Technical User
Mar 20, 2002
85
0
0
US
I am setting up a method to program IP phones. I need a way to query the local network, get the MAC addresses of all the phones. I so far have this to discover the phones I want:

arp-scan --interface=eth0 --localnet --retry 1 --timeout 50 --numeric --plain | grep -e 80:82:87

This shows from the command line only the devices I want. From here I want to show them on a page and also send the info - just the MAC address to a Mysql table.

Where do I go from here??

Thanks for any hints or direction

 
What do you mean where do you go?

If you already have a command that returns the data, then what is it you don't know how to do?

Running it from PHP?
Outputting to website? echo, or print is used to output to screen.

Storing it in a DB? Insert into table...


The next step depends on what you want to do with that command, or where you are stuck.









----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
AND;

How is a Linux/Unix shell command
Code:
arp-scan --interface=eth0 --localnet --retry 1 --timeout 50 --numeric --plain | grep -e 80:82:87

Anything to do with PHP?

Forum619 or forum54

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
I guess I didn't explain what I want to do correctly. I want to use that command with the exec function and put the results in a DB. I would be running the command from a php page
 
Okay;

So which, but of:

'exec' the shell command

'catch' the returned data as a variable $retval = shell_exec($command);

and construct a MySQL query from $retval is it you have problems with?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
I got the info to print to the page in an array using $result in the command. and using print_r
So using your suggestion I should be able to create a query to inset the results.
 
Yes. Once you have the data in a PHP script, you can use it to build the query.

The question we were asking is what exactly you were having trouble with. So we'd know exactly what to help you with.

You laid out the steps, so which one do you need help with?

You already have the exec command running, and output to screen. so the next step is to take the data and build a query.


If you give us an example of the data you are getting back, and what your MYSQL table format looks like we can help you build a query if that's where you are stuck.






----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Thanks vacunita

The result of the command I mentioned earlier sends this to the page;

Array ( [0] => 10.1.10.76 80:82:87:03:9d:38 ATCOM Technology Co.Ltd. [1] => 10.1.10.84 80:82:87:03:d8:58 ATCOM Technology Co.Ltd. [2] => 10.1.10.171 80:82:87:03:63:e6 ATCOM Technology Co.Ltd. [3] => 10.1.10.235 80:82:87:03:19:f0 ATCOM Technology Co.Ltd. )

I have a simple table with an id(Primary) auto and a text field for the mac address. That is all I need at this point.
 
So this is working:

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

and it prints to the screen:

10.1.10.76 80:82:87:03:9d:38 10.1.10.84 80:82:87:03:d8:58 10.1.10.171 80:82:87:03:63:e6 10.1.10.235 80:82:87:03:19:f0
 
explode the returned string using a space as delimiter.

Parse the resultant array with a foreach loop and insert each element into your table.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Since it appears to be already in an array, just loop through the array, and build the insert statements.

Code:
Mysql_connect(...); //connect to database.
Foreach($retval as $key => $item)
{
$temp = explode(' ',$item); // explode the up/Mac string to get the MAC address.
$mac = $temp[1]; // put the extracted MAC address in a variable. 
 $qry = "Insert into tablename macaddressfiledname VALUES($mac)"; // build insert query

$res = mysql_query($query); // run constructed query to instar values;

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Since it appears to be already in an array, just loop through the array, and build the insert statements.

Sure, I was considering handling the grep' output, due to this requirement.

"also send the info - just the MAC address to a Mysql table."

so

egrep -ho [0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:

egrep -ho "[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}" ./*

which can be shortened to

(([[:xdigit:]){2}:?){6}



Will get ONLY the MAC addresses (six sets of two hex digits delimited with a colon.) as your grep assumes the MAC address always starts with or contains 80:82:87

Added after edit ... one I will figure that the preview button may not be where I expect it to be, but the submit button is.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top