Exec node isn't working correctly

Hello community,

I wrote a script, which edits a file and copies it into another folder.
If I execute it myself it works just fine and does its work but as long as Node-Red is executing it with the exec node, the files content isnt changing. I dont get any error messages etc. (not even something about permissions).

I want the user to change the network settings in the dashboard, easily with a form. But for debugging I'm just using some random parameters:

image

Below you can see the script so you get a point what im trying to do.

#!/bin/bash

##############################################################################

mask2cidr() {
	nbits=0
	IFS=.
	for dec in $1 ; do
		case $dec in
			255) let nbits+=8;;
			254) let nbits+=7;;
			252) let nbits+=6;;
			248) let nbits+=5;;
			240) let nbits+=4;;
			224) let nbits+=3;;
			192) let nbits+=2;;
			128) let nbits+=1;;
			0);;
			*) echo "Error: $dec ist not recognised"; exit 1
		esac
	done
	echo "$nbits"
}

##############################################################################

ip=$1
netmask=$2
gateway=$3

numbits=$(mask2cidr $netmask)

cat << EOF > dhcpcd.conf

# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

#auto eth0
interface eth0
static ip_address=${ip}/${numbits}
static routers=$gateway


# Example static IP configuration:

#interface eth0
#inform 10.207.40.10/24

EOF

sudo rm /etc/dhcpcd.conf
sudo cp /home/pi/NetConfig/dhcpcd.conf /etc/

exit 0

Kind regards

Justin

Unless Node-RED is running as root it won’t have permission to write files to /etc directory

I also tried it with commenting this section out.. without success.

It wont even edit the file thats in the same folder as the script

Feed all the outputs of the exec node into separate named debug nodes set to output complete message and show us what you get.

Also does the script ask for a password when you run it in a terminal?

No, it doesn't ask for a password. I also tried to execute it with sudo in the exec node..

Here's the output:

image

What about output 3?

Output 2 isn't responding because I dont get any errors

I presume you mean output 3 is not responding.

Which folder do you expect the file to be written to, and why?

No, I'm pretty sure it's output 2:

Writing to

/home/pi/NetConfig/dhcpcd.conf

so we shouldn't get in trouble with permissions in this moment.

I also did some echos in the script to check if the parameters are coming in and it's even getting executed. Everything looking normal, just the values arent set in dhcpcd.conf

The folder-structure:
image

I think you could have named the debug nodes more helpfully. 1, 2, and 3 would have been more obvious.

Why do you expect it to write to that directory? Add a pwd to the script, or better, put the full path to the file, as you have when you copy it.

1 Like

Yep, the failure was inside the script. I had to give the full path:

cat << EOF > /home/pi/NetConfig/dhcpcd.conf

Seems like it wrote all the time to /etc/dhcpcd.conf

I thought it would just write to the file, which is in the same folder as the script.
But well, now I've learned something new :smile:

Thanks for the help!

I think more likely /home/pi/.node-red or somewhere like that.

We both are wrong.. No changes. Neither in /etc/ nor in /.node-red/

In the future I will just give the directory where ever I can :joy:

1 Like

Out of interest, add a pwd command to find out.

1 Like

Guess what, theres a file called dhcpcd.conf right inside /home/pi :man_facepalming: :smile:

1 Like

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