Possible to get ip address from mac address ?

975 views
Skip to first unread message

Toshi Bass

unread,
Jan 18, 2017, 9:22:23 AM1/18/17
to Node-RED
Using node-red is there a way to find the ip address for a device for the known mac address of the device.

I mean I know the mac address of my device and that doesnt change but on reboot of my router the ip address can change I have tried loads of ways to fix the ip address of the device (esp8266 based) none of which have been successful, and I don't really care what ip address my device has but I do need to ping the ip address to confirm its connected .... so what I am looking to do is write a flow that gets the ip address from a given mac address, any pointers to get me started would be appreciated.

Colin Law

unread,
Jan 18, 2017, 9:28:07 AM1/18/17
to node...@googlegroups.com
Can you not configure the device to have a fixed address?

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/ce1d4e4c-f82e-48cb-a3ee-0183e87b2daa%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Jan 18, 2017, 9:50:29 AM1/18/17
to node...@googlegroups.com
most routers will allow you to allocate an ip to a known mac address. If you can't do that - then investigate the mDNS library for the esp8266 - https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266mDNS
then you can ping your device as  my-device.local 

Ben Hardill

unread,
Jan 18, 2017, 11:08:00 AM1/18/17
to Node-RED
Do a arp lookup?

Assuming the device has sent a packet on the same network as the device doing the lookup it should be able to give you a mac to IP address mapping

Toshi Bass

unread,
Jan 18, 2017, 12:21:04 PM1/18/17
to Node-RED
Colin Dave yes I can allocate ip address of known mac address on my router but it doesn't work because esp has already generated a ip address and if I try to set a static address on the esp it screws up OTA so for example when I flash my esp with basic OTA script esp assigns ip 192.168.0.21 which is the ip address stored on the esp in flash memory) I then use that address to flash my script to the esp .. usually this address stays the same but if the router restarts it can change to say 192.168.0.23 if I add code to assign a static ip address say 192.168.0.250 on the esp then I can OTA flash to this new code using 192.1698.0.21 and indeed that sets the ip to 192.168.0.250 but the arduino ide does not reconise ...250 so I can no longer flash new code and if I reset the esp for reasons beyond me the esp reverts to ...21 even though the code is there to change the ip to ...255    Ive had my fill with trying to resolve this its just too frutrating.

Ben arp -a appears to be what I was looking for.....

[{"id":"8c061f5f.2d622","type":"inject","z":"8ca461ee.d26bc","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":163.75,"y":1665,"wires":[["cbcdd20d.7403f"]]},{"id":"cbcdd20d.7403f","type":"exec","z":"8ca461ee.d26bc","command":"arp -a","addpay":true,"append":"","useSpawn":"","timer":"","name":"","x":400,"y":1674.375,"wires":[["baf6aee4.085dc"],[],[]]},{"id":"baf6aee4.085dc","type":"debug","z":"8ca461ee.d26bc","name":"","active":true,"console":"false","complete":"false","x":642.5000076293945,"y":1638.750023841858,"wires":[]}]

I just need to extract the information that I need from the result, which my be a little beyond me but Ill give it a go. thanks for your help

Julian Knight

unread,
Jan 21, 2017, 11:14:08 AM1/21/17
to Node-RED
You need to make sure the ESP is doing DHCP which should be the default and also make sure that the default gateway is set to your router. Make the static DHCP entry in your router.

To answer your initial question, you use an arp lookup. Here is an example I use on my own setup (running on a pi). I use a custom macfile to give me proper recognition of some of the devices I use, the macfile can be used to identify individual mac addresses by name as well as generic interfaces, it returns the ip address as well. You need to make sure that the user running node-red is able to sudo arp-scan without a password.

[
   
{
       
"id": "ee17c8c9.3af858",
       
"type": "inject",
       
"z": "c919054f.36e6f8",

       
"name": "",
       
"topic": "",
       
"payload": "",

       
"payloadType": "date",
       
"repeat": "300",
       
"crontab": "",
       
"once": true,
       
"x": 120,
       
"y": 1305,
       
"wires": [
           
[
               
"aa8e0847.ed13d8"
           
]
       
]
   
},
   
{
       
"id": "aa8e0847.ed13d8",
       
"type": "exec",
       
"z": "c919054f.36e6f8",
       
"command": "sudo arp-scan --localnet --macfile=/home/pi/node/nrlive/mac-vendor-jk.txt",
       
"addpay": false,

       
"append": "",
       
"useSpawn": "",
       
"timer": "",

       
"name": "ARP Scan (sudo)",
       
"x": 290,
       
"y": 1305,
       
"wires": [
           
[
               
"d66c29e1.3f3718"
           
],
           
[
               
"3c6a062f.6b7bfa"
           
],
           
[
               
"3c6a062f.6b7bfa"
           
]
       
]
   
},
   
{
       
"id": "dde7941.059d168",
       
"type": "comment",
       
"z": "c919054f.36e6f8",
       
"name": "Use arp-scan to track active IP devices",
       
"info": "",
       
"x": 216,
       
"y": 1238,
       
"wires": []
   
},
   
{
       
"id": "d66c29e1.3f3718",
       
"type": "function",
       
"z": "c919054f.36e6f8",
       
"name": "",
       
"func": "\n// Record the date/time of the response\nvar timestamp = new Date();\n\n// Split the response into lines\nvar lines = msg.payload.split(\"\\n\");\n\n// Trim unwanted lines (pop takes off the end, shift off the start)\nlines.pop();                 // Last line is empty\nvar info = lines.pop();   // Tells us what happened and how long it took \nlines.pop();lines.pop();     // 1 empty and 1 unneeded line\nlines.shift();lines.shift(); // 2 unneeded lines at start\n\n// For each of the returned responses\nfor (var line of lines) {\n    // line is tab separated\n    // IP Address, MAC Address, Device/Manufacturer Name\n    var data = line.split(\"\\t\");\n    \n    //devicescan,mac=e4:98:d1:47:1d:70,device=Microsoft\\ Mobile\\ Oy value=1,ip=\"192.168.1.106\"\n    data[2] = data[2].replace(' ', '\\ ').replace(',', '\\,').replace(\"'\", '');\n    node.send([{'payload': \n        'devicescan,MAC=' + data[1] + ' value=1,IP=\"' + data[0] + '\",' + 'Name=' + data[2]\n    }, {\n        'payload': {\n            'timestamp': timestamp,\n            'IP':   data[0],\n            'MAC':  data[1],\n            'Name': data[2]\n        },\n       'topic': 'DEVICES/ARP/' + data[1]\n    }]);\n}\n\n// \"Ending arp-scan 1.8.1: 256 hosts scanned in 2.904 seconds (88.15 hosts/sec). 14 responded\"\ninfo = info.replace(\n    /Ending arp-scan .*\\: 256 hosts scanned in (.*) seconds \\(.* hosts\\/sec\\)\\. (.*) responded/, \n    replacer);\n    \nnode.status({'text': info });\n\n//return msg;\n\nfunction replacer(match, p1, p2, offset, string) {\n    return p2 + ' responses in ' + p1 + ' seconds';\n}",
       
"outputs": "2",
       
"noerr": 0,
       
"x": 470,
       
"y": 1285,
       
"wires": [
           
[],
           
[
               
"df18df28.d8765",
               
"7f19a739.f455f8"
           
]
       
]
   
},
   
{
       
"id": "3c6a062f.6b7bfa",
       
"type": "debug",
       
"z": "c919054f.36e6f8",
       
"name": "",
       
"active": false,
       
"console": "false",
       
"complete": "true",
       
"x": 470,
       
"y": 1365,
       
"wires": []
   
},
   
{
       
"id": "7f19a739.f455f8",
       
"type": "mqtt out",
       
"z": "c919054f.36e6f8",
       
"name": "DEVICES/ARP/<mac>",
       
"topic": "",
       
"qos": "1",
       
"retain": "false",
       
"broker": "3b2726f7.c4d8da",
       
"x": 660,
       
"y": 1343,
       
"wires": []
   
},
   
{
       
"id": "3b2726f7.c4d8da",
       
"type": "mqtt-broker",
       
"z": "",
       
"broker": "localhost",
       
"port": "1883",
       
"clientid": "Pi2_NR-Live",
       
"usetls": false,
       
"verifyservercert": true,
       
"compatmode": false,
       
"keepalive": "15",
       
"cleansession": true,
       
"willTopic": "DEVICES/PI2NR-LIVE",
       
"willQos": "1",
       
"willRetain": "false",
       
"willPayload": "Offline",
       
"birthTopic": "DEVICES/PI2NR-LIVE",
       
"birthQos": "1",
       
"birthRetain": "false",
       
"birthPayload": "Online"
   
}
]
Reply all
Reply to author
Forward
0 new messages