The best (and more reliable) way to notify NR from DOS .bat script

Hi, I've a program that may alert me executing a .bat, I've some ideas how to advice NR about that, for example I can send something by telnet/SSH, or create a file which NR can advice and (then) delete.
I would like to use TCP to be sure the packet will not be lost, maybe there is already a module with an .exe file that is able to catch when it's started. Thank you!

So what I've done previously - on Linux cmd line rather than Windows though - is to use a command line web tool like wget to reference a URL defined in Node-RED. This is easy and reliable.

You should be able to do this from a Windows CMD prompt too by grabbing a Windows version of wget or similar. You can certainly do it from PowerShell without any extra software.

2 Likes

thank you, what you exactly triggered on side NR? You used a particular node?

I have a couple of BASH scripts that run on CRON schedules and that post notifications back to Node-RED on completion.

Here is one. It runs an nmap scan to get devices on my home network, in this case, the output is saved to an XML file since nmap can do that but you could also use curl or wget to send data as well. In this case, the flow triggered in Node-RED reads the file and updates some data retained in Node-RED as a catalogue of known and discovered devices and it also sends updates to MQTT.

#! /usr/bin/env bash
# Fast scan the local network for live devices and record
# to /tmp/nmap.xml which can be used in Node-RED
#
# To run manually:
#   sudo /home/home/nrmain/system/nmap_scan.sh
#
# To run via cron:
#   sudo crontab -e
#       01,16,31,46 * * * * /home/home/nrmain/system/nmap_scan.sh

# Run the scan
nmap -sn --oX /tmp/nmap.xml --privileged -R --system-dns --webxml 192.168.1.0/24
# Make sure ownership & ACLs on the output are secure
chown root:home /tmp/nmap.xml
chmod --silent 640 /tmp/nmap.xml
# Trigger the Node-RED update
curl -I 'http://localhost:1880/localnetscan'

I have curl on Windows. But PowerShell can do a web call natively.

1 Like

Thank you! I'm interested exactly which module you triggered in NR? I need to otput a message and process that mesage in other modules placed in flow... how can I output it? Thanks

OK, here is the flow:

Obviously, it is the top section. The http-in is the trigger of course. That firstly reads that file and in parallel gives an http-response since there isn't really an actual response to the calling script in this case.

Once the file is read, it is processed. The network-out link node goes to 2 separate uibuilder nodes, live and test where some extra info can be edited. The flow segment with the split sends the info to MQTT.

1 Like

Alternatively use MQTT, my own choice it would be

1 Like

That's interesting, the script using curl to tell Node-red there is data for it.

But can you explain why you trigger the script externally to Node-red rather than via an inject and the exec node?

In fact why does the Bash code reside in an external script file rather than inside Node-red?

1 Like

You can't just inject and exec node, you need to have event which does this, it can be http-in or mqtt event as described above.

Largely because the script requires elevated rights and I don't want Node-RED to have those rights. So the script runs off the root CRON and triggers Node-RED. You will note that the script messes with the permissions on the output file so that it is available to Node-RED, it wouldn't be otherwise. I could, of course, have POSTed the data using curl but I couldn't be bothered. :slight_smile:

Just because you have a nice hammer, not everything becomes a nail. :grinning:

Mostly for the above reason. But some things are simply easier in a dedicated script. People tend to loose sight of alternatives when they get into Node-RED but it isn't always the best tool for the job.

Ah that makes sense thanks.

Indeed. Node-red, like the Linux shell itself, is very capable to screw together OS utilities, executables of all kinds and it's own core and contrib nodes.
But it is convenient (eg portability) for scripts needed for a flow to be stored in that flow and executed from there.
Also Node-red gives a convenient mechanism for triggering events at specific times.

Apologies to the OP for drifting off Windows to Linux :slightly_smiling_face:

or to screw UP :rofl:

And sometimes I do indeed do that, as in these examples:

But not always.

Several in fact. And I certainly use them as well. But there are often enough things that are easier and more secure and robust to do outside of Node-RED. Things, for example that you want to reliably continue even if NR fails or has to be updated/restarted for any reason.

2 Likes

Yes, agree, and then, what I do, I monitor those outside services & scripts that they do their stuff, and that they are alive, and if not, they do get restarted, yes, by Node-RED :wink:

So many options and possibilities...

2 Likes

Hi mates, thank you for developing this thread and sharing your experience.
I like the http in solution, uses TCP, looks reliable and easy to implement.
So I've tried what suggested, curl -I 'http://localhost:1880/http-in-node' I receive the message on NR and that's ok but I've 2 problems:

  1. on DOS the curl process is waiting for something, not terminating, I would like curl push some content to NR to identify itself properly and then quit how to do that?
  2. on NR I would read that property pushed and identify the request
  3. Do I have a way to monitor and log if curl wasn't sucesful? For example for any reason port 1880 isn't open, or http handshake wasn't sucessful, does curl returns an error?

Thank you!

Have you added an http-response node set to return 200? Note how I do that in my flow.

By default, you will be using a "GET" HTTP transaction so you can add query parameters to the URL, you can access them in Node-RED. If you need something more complex or secure, you will need to look up how to make curl do a POST transaction and how to pass JSON in the body of the POST.

This is much easier on Linux than Windows. You probably could create a custom Windows Event log though complex command line stuff is much better done with PowerShell rather than command prompt.

Anything is possible :slight_smile: just not always easy.

1 Like

thank you, solved the first 2 placing HTTP response node with 200, and adding to url http://localhost:1880/http-in-node?propertyinpayloadmsg=stringvalue'
This is lovely solution
Soon I'll try to manage errors and log them

1 Like