Programmatically check port status with npm portscanner

To check if a port is open or closed on some device I usually just open the firewall settings or use the command line, but shouldn’t there be some way to get that information programatically with Node-RED?

To my surprise I couldn’t find a node package to just do this for me, but I did find the npm portscanner package; this package has a function checkPortStatus that returns “open” or “closed” for a specific port on a specific device.

Unfortunately it doesn’t have a Node-RED package, meaning there’s no default way to install or interface with the package, so I turned to node-red-contrib-npm. This node is a generic wrapper for other npm packages, it was designed to add support for npm packages like portscanner that don’t have their own nodes, which is exactly what we need here. To get it going all you need is the package identifier from the npm registry, the function you want to call, and the parameters that the function reads in – in this case checkPortStatus reads in device IP and a port number, then outputs that port’s status.

Here is how I configured the npm node to execute portscanner’s checkPortStatus function:

Now we just need to give this node the port number to check and the host name or IP address of the specific device, the msg.payload format that I found works is:

{ "port":"22", "host":"localhost" }

Just change 22 with your port number and localhost with the IP address or hostname of the device of interest.

Here I am checking my SNAP PAC controller’s ports from Node-RED running on my groov EPIC:

The debug pane on the right shows that checking port 2001 or 22001 gives me the result “open”, which matches my currently firewall settings, but port 22 is closed, since the SNAP PAC has not been configured to open that port for anything – so the results are exactly what I expected.

Below is an example flow you can import to try this out for yourself, it checks the local device (you can use “localhost”, “127.0.0.1”, or the device host name) to see if ports 22, 22001, 8443, 1883, or 1234 are open by showing their status in the debug pane.

Don’t forget to install node-red-contrib-npm through the pallette manager before installing, and as always, happy coding!


[{"id":"fd57ae4.f9c865","type":"npm","z":"e02f10b4.235de","name":"portscanner","func":"// NPM module exposed as variable, npm_module\nreturn npm_module(msg.payload);","npm_module":"portscanner","module_style":"function","msg_payload":"return_val","function_name":"checkPortStatus","x":470,"y":1340,"wires":[["8e5bbdab.d53f6"]]},{"id":"bfd8bdc4.4fcc4","type":"inject","z":"e02f10b4.235de","name":"port 443","topic":"port 443","payload":"{ \"port\":443, \"host\":\"127.0.0.1\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1340,"wires":[["fd57ae4.f9c865"]]},{"id":"8e5bbdab.d53f6","type":"debug","z":"e02f10b4.235de","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":1340,"wires":[]},{"id":"cede5f1e.9f955","type":"inject","z":"e02f10b4.235de","name":"port 22001","topic":"port 22001","payload":"{ \"port\":22001, \"host\":\"localhost\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1300,"wires":[["fd57ae4.f9c865"]]},{"id":"79dee8bd.181bb8","type":"inject","z":"e02f10b4.235de","name":"port 22","topic":"port 22","payload":"{ \"port\":22, \"host\":\"localhost\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1260,"wires":[["fd57ae4.f9c865"]]},{"id":"bd0a4d41.7bbdb","type":"inject","z":"e02f10b4.235de","name":"port 1883","topic":"port 1883","payload":"{ \"port\":1883, \"host\":\"127.0.0.1\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1380,"wires":[["fd57ae4.f9c865"]]},{"id":"42cd3ecc.facdd","type":"inject","z":"e02f10b4.235de","name":"port 1234","topic":"port 1234","payload":"{ \"port\":1234, \"host\":\"127.0.0.1\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1420,"wires":[["fd57ae4.f9c865"]]}]

1 Like