Question

How can I setup a PHP socket connection using MAC address instead of IP address?

Asked by: dossbob

I need to communicate with several (25-50) specialized devices from a PHP-based webpage. I have a working prototype which uses socket_connect, but that approach requires me to know the IP address of the device.

My client wants to let DHCP assign the device IP addresses so he doesn't have to keep track of static IP addresses.

Is there a way in PHP to find the current IP address for a specific MAC address?

Alternately, I can broadcast a command for all devices on the network to send me their MAC address. Is there a way in PHP to extract the IP address from the Ethernet packet the devices send me?

(I know this can be done in VB, so there SHOULD be a way in PHP)

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-30 at 09:28:35ID24693229
Tags

PHP

,

sockets

,

MAC address

Topics

TCP/IP

,

PHP for Windows

,

Dynamic Host Configuration Protocol (DHCP)

Participating Experts
2
Points
0
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PHP SOCKETS
    Hi, My PHP page should communicate with a VB application on a remote server. I know it's using socket programming but can somebody please provide me a sample code for doing so. Any help would be greatly appreciated. Eapen
  2. socket packet in PHP ?
    im new in PHP . I want to know socket packet in PHP . Can some one explain ? THanks

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: angelIIIPosted on 2009-08-30 at 09:35:00ID: 25218241

could this script help you?
http://webscripts.softpedia.com/script/Networking-Tools/php-DHCP-hosts-list-32048.html


what oS? for windows client, you could check this command line:
netsh -c dhcp server scope 192.168.10.11 show clients

 

by: dossbobPosted on 2009-08-30 at 11:07:14ID: 25218540

angelIII,

Thanks for the quick response -- but I need to do this programmatically.

My PHP program needs to scan the devices every few seconds and continuously update a visual representation of the device status.

It needs to be able to recognize if DHCP has changed the IP addresses for any of the devices, and change that device's socket connection to its new IP address.      

 

by: angelIIIPosted on 2009-08-30 at 11:25:10ID: 25218604

I understand that.
but first things first: can you check out if any of the 2 above suggestions can make it into (your) php code.
means: first try with 1 device manually with code doing just that.
after that, we will think about how to make this happen for the global picture.

 

by: RQuadlingPosted on 2009-08-30 at 12:38:38ID: 25218943

What is the VB technique you have?

 

by: dossbobPosted on 2009-08-30 at 14:16:15ID: 25219424

RQuadling:

I originally assumed that the IPPacketInformation.interface property would contain the MAC address, but have been unable to verify that. This may not be any easier in VB than PHP.

angelIII:

I'll have a look at the script you suggested tomorrow morning and let you know what I find.

When I tried the command on my XP Home box, I got "The following command could not be found: dhcp". According to the "help" info for "netsh", the only allowable sub-contexts that can follow the -c switch are:

   bridge diag firewall interface lan ras routing winsock  
   

 

by: RQuadlingPosted on 2009-08-31 at 01:40:25ID: 25221279

If you load up wireshark (an IP packet sniffer/analyser), you will see that the mac address is always the closest point away from you. I'm on a wireless link, so the mac addresses I see are my laptop's and my router. That's it. No more details in the actual packet received.

Wireshark shows the following details (for a random packet) ...

Frame 1 - Can see that the packet is an eth:ip:tcp:ssl packet - essentially an https packet - I've got GoogleMail open over https.
Ethernet II - The data in this element is actually the first 14 bytes of the packet. It contains the mac addresses of the source and destination - essentially the hardware route. That's all that is needed as the router will look after which port to use for the IP address routing table it is maintaining.

After that it is IP data (20 bytes) and then TCP data (20) and then the SSL data (1430 bytes).

That's it. All done.

So, unless you've got ways of tracking the packets through each of the routers from the source to you ...

Basically the answer is no. The mac address you get is always going to be for the local hop only.

 

by: dossbobPosted on 2009-09-06 at 12:07:27ID: 25271072

I've given up on this idea.

I never found a way to make it work, but even if I eventually did, it would be so "non-standard" (nice way to say "kludgy") that I wouldn't want to rely on it.

I've talked my client into using static IP addresses assigned to the devices at installation. That way I can use sockets they way they were designed to be used.

I consider this question closed, but I don't know how to close it without awarding points.

 

by: angelIIIPosted on 2009-09-06 at 12:10:35ID: 25271083

you can either delete the question, or accept your own comment as answer...

 

by: RQuadlingPosted on 2009-09-14 at 16:08:22ID: 25330118

If you don't want to set static IPs on each device, but have a list of MAC addresses, you COULD use DHCP to assign an address based on MAC address. This saves the manual configuration per device - some device's have no easy interface, so doing it in DHCP is probably easier.

 

by: dossbobPosted on 2009-09-14 at 16:29:34ID: 25330207

How would that work?

Would I somehow tell DHCP a MAC address and an IP address to assign to it?

If not, how would I know which IP address DHCP decided to assign to the MAC address?

 

by: RQuadlingPosted on 2009-09-15 at 01:05:17ID: 25332749

The idea is that you can have fixed IP addresses without the need to manual program each device - some devices require dismantling to do this or connecting via a serial port - often a PITA to do.

By telling DHCP to reserve the IP address for a specific mac address, the IP address is managed by the DHCP server.

If the reserved addresses are in a fixed range, then you can easily iterate the addresses.

Not really a full solution, but just a few comments which hopefully make it easier to set up.


Open box.
Remove device.
Read mac address.
Enter mac address for reservation in DHCP in reserved IP range.
Put device into use.

Is it any different to fixing the address in the device?

Yes.

You would STILL need to remove the address from DHCP to stop DHCP from giving the address out to another pc or printer or device which can handle DHCP.

I'm just saying let DHCP do its thing.

 

by: dossbobPosted on 2009-09-15 at 10:29:44ID: 25337212

That would work, as long as the DHCP server has the ability to assign a specific IP address to a specific MAC address. (My Linksys router is my DHCP server,  but it doesn't appear to have that feature).

But I might as well assign the IP address to the device in my application program.
It's a simple command, and I already have to manually maintain an IP-to-MAC table in the application anyway.

If I let DHCP assign them, I have to maintain the IP-to-MAC association in both places, risking a careless mismatch.  

 

by: RQuadlingPosted on 2009-09-16 at 00:55:38ID: 25342961

I assumed you had something a little bigger than just a router doing the DHCP.

"If I let DHCP assign them, I have to maintain the IP-to-MAC association in both places, risking a careless mismatch."

Not true if you had a DHCP server which allowed you to reserve static IPs for specific MAC addresses.

I use this for collecting usage data from our networked scanners and printer.

All scanners are in 10.50.10.x and printers are 10.50.20.x ranges.

As a new printer or scanner is added to the network, the engineer takes the mac address off the back of the device and logs it in the DHCP server as the next reserved address. Job done.

The engineer lets the stats aggregator code know about the new device (essentially a web form) which scans the reserved range for new IPs. The only place anything is entered is in the DHCP server.

But, at some level, an IP-to-MAC table is maintained.

It MAY be worth using a different DHCP server (software based maybe) if this mechanism is suitable.

 

by: dossbobPosted on 2009-09-16 at 06:27:56ID: 25345326

NOTE: this is a long post, but it's NOT meant to argue with your comments. Instead, I hope this more complete explanation of the problem might trigger new ideas, or a further explanation of something I might misunderstand.

@@@@@@@@@@@@

In your scenario, you add a new printer  in Office 203 with MAC = "MMM".
The engineer tells DHCP to reserve IP address of 192.168.1.101 for the printer with MAC = "MMM". HIS job is done.

Six months later, a programmer writes an application which must print to the printer in Office 203. She doesn't know its IP address, but that doesn't matter because she knows its network name. She writes to printer "Office203_Printer". HER job is done.

But my devices don't have network names. I need to write to them with PHP's "socket_write" function. If I'm correct, the ONLY way to do that is to connect to the socket with a command like this:
 
     "socket_connect(<socket>, <device IP address>, <device port>)"

Within the application program, I don't know the <device IP address> for the device in Office 203.

I can fix that by maintaining an "IP-to-DeviceLocation" table which my application program can read. When I want to write to "Office203_Device", I can look up its IP from the table.

If I already have to maintain that table, I can add the MAC address to it. My application can then look up the new device's IP addresses by its MAC address and download the IP address to the device.

I realize I'm doing work that DHCP could do, but I have to have the application table anyway to know the physical location of a specific IP address. If I maintain IP-to-DeviceLocation in my table AND maintain IP-to-MAC in DHCP, I have to maintain 2 tables which can get out-of-sync.

I BELIEVE that the simplest solution is to
   1. Reserve a range of IP addresses (ONCE) in DHCP
   2. Maintain an application table with IP, MAC, and DeviceLocation
   3. When a new device is added to the network, first add it to the application table, then use the MAC address from the table to set the device's IP address to the one in the table.
   4. When my application needs to write to a specific device, it uses the DeviceLocation to look up the associated IP address, then writes to that IP address.

@@@@@@@@@@@@

If I've misunderstood something in the previous suggestions, please set me straight. If not, I hope this wordy explanation will trigger some better ideas -- I really don't want to ask my client to maintain an application table, but so far I don't see a way around it.  

 

by: RQuadlingPosted on 2009-09-16 at 07:02:09ID: 25345653

Why have a "device location" or a "network name" - you're a programmer - you only need the IP. Sure, they are useful for users who may have heard of IP addresses, but probably not MAC addresses.

For your app, the IP is fine.

It comes down to how easy it is to configure the IP address on the device.


So, DHCP to reserve block of IPs for devices.
New device arrives.
Allocate IP to new device.
Use IP in your app.





There are plenty of GUI apps around to find the IP/MAC addresses.

But I did come across a fast discovery method which is just a simply batch script.

Essentially, it pings every IP in the required range (shown as 10.0.0.x).

Once pinged, the local machine has mapped each IP to a MAC address.

The ARP command displays them (well below logs them to a file for you to process as you want).

If the devices all get an IP on a different subnet, then you should be able to filter things.

@ECHO OFF
FOR /L %%I in (1,1,254) DO START /MIN /LOW PING -n 1 10.0.0.%%I
PAUSE
ARP -a > C:\IP.LOG
                                              
1:
2:
3:
4:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...