Skip to content

Instantly share code, notes, and snippets.

@swina

swina/README.md Secret

Created December 29, 2019 23:04
Show Gist options
  • Save swina/18074fd68e0918a40cf6b6144d0d5b4d to your computer and use it in GitHub Desktop.
Save swina/18074fd68e0918a40cf6b6144d0d5b4d to your computer and use it in GitHub Desktop.
Scan LAN/Subnet for devices

Scan LAN/Subnet for devices

This simple flow uses nmap to scan for devices in a subnet.

Usage

Edit the scan subnet and change your the network subnet with your network ip. By default the value is 192.168.2.0/24.

The command to execute (Linux tested) requires nmap installed on machine running node-red.

sudo nmap -sn 192.168.2.0/24 | awk '/Nmap scan report for/{printf $5;}/MAC Address:/{print "|"substr($0, index($0,$3)) }' | sort

Scanning

The flow has an inject node (runs every minute) that trigger the networks scan.

Output

Please pay attention that the response time of the scan depends on the size of your network (how many devices attached).

I added a parser for the scan result that transform data to an array of objects

[

   {
      ip: 'device_ip',
      mac: 'device_mac_address,
      brand: 'device_brand'
   },
   {
      ....
   }
]

[{"id":"c3961d41.0f858","type":"tab","label":"Scan Subnet for Devices","disabled":false,"info":""},{"id":"842fa7ed.314678","type":"inject","z":"c3961d41.0f858","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":60,"wires":[["c43bcf07.4070d"]]},{"id":"c43bcf07.4070d","type":"exec","z":"c3961d41.0f858","command":"sudo nmap -sn 192.168.2.0/24 | awk '/Nmap scan report for/{printf $5;}/MAC Address:/{print \"|\"substr($0, index($0,$3)) }' | sort","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"scan subnet","x":370,"y":60,"wires":[["6903a852.c6bd58"],[],[]]},{"id":"6903a852.c6bd58","type":"function","z":"c3961d41.0f858","name":"Subnet Devices Array","func":"let response = msg.payload.split('\\n');\nlet found = []\nlet device\nresponse.forEach ( line => {\n if ( line.indexOf('|') > -1 ){\n device = {\n ip : line.split('|')[0],\n mac: line.split('|')[1].split(' ')[0],\n brand: line.split('|')[1].split(' ')[1]\n }\n found.push ( device )\n }\n})\nmsg.payload = found;\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":60,"wires":[["c5c606d2.36a7f8"]]},{"id":"c5c606d2.36a7f8","type":"debug","z":"c3961d41.0f858","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":850,"y":60,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment