How to execute a commandline with Node-red

Hi,
I've read a lot of posts about my question, but haven't found the solution yet.
I am very new to the Node-Red and Js domain and am trying to do a simple project.
I found an example of uploading a file with the dashboard that suits me but I also want to execute a command for the mosquitto server to publish the file, that is, to execute the command
"mosquitto_pub -h 192.168.2.100 -p 1883 -u auser -P apassword -t uploads/192.168.2.101 -f /tmp/build*.tmp/afile.bin".
I tried a node exec but I failed.
I understand it's a very simple matter for someone who knows a little bit, but I'm not one of them. If anyone wants to help me I would appreciate it. Thanks

Rather than executing mosquitto_pub (which you could do with an Exec node, but then your user and pwd would exist in plain text in the flow, which is not good) have a look at the MQTT nodes which allow you to subscribe and publish to mqtt brokers such as mosquitto.
I think they are installed as part of the core nodes should already be there in the palette.

[edit] Actually I am not sure whether you can publish a file like that with the MQTT nodes, someone else may perhaps comment on that.
What was the problem with the exec node? Look on all the outputs of the exec node to see the error. You might need to provide the full path to mosquitto_sub (somwthing like /usr/local/bin/mosquitto_sub, depending on your OS.

@colin - MQTT just transfers bytes from one place to another so yes you can send files... use a file-in to read the file - set it to deliver a buffer - and feed straight into the mqtt out node - (and set that with the appropriate topic, user, password, etc)

Thanks, that what I thought, but hadn't seen the -f option with mosquitto_sub before, so thought I should better not pronounce lest I was wrong.

The -f option wouks fine in an exec node, you just have to provide the correct path to the file.

thanks zenofmud for your help.
I tried your suggestion with the node file-in and it works but with a fixed file name.
Could not find how to set the filename to
$env('UPLOAD_DIR') & '/' & req.files[0].originalname
(from a change-node's output) that I want

Um, Dave offered that suggestion :grin:
So where/how are you setting $env('UPLOAD_DIR')?
where is req.files[0].originalname coming from?
What platform and OS are you on?

Sorry Dave!
$env('UPLOAD_DIR') & '/' & req.files[0].originalname is the msg.payload of a change-node that takes the file I choose, and the OS is raspberryPi's OS (rasbian lite).
The file-in node takes a fixed filename not a variable as I want. How can I force the file-in node to get a variable filename ?

Look in the info tab for the node and you will see that you can pass the filename in msg.filename.

I'm sorry but i'm stupid.
I can't see where can insert "msg.filename".
This is the screen hardcopy i have openning info tab

To tell the node to read the file you have to send it a message, in that message you need to set msg.filename to the required name.
In the node config screen itself (when you double click the node to open it) leave the filename field empty.

When it says "if not set in the node configuration" it means if you haven't filled in (set) a fiename.

Many thanks Colin, now it works as i needed!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.