I'm trying to change the static IP address of my PI from a node red dashboard. I found this link
https://flows.nodered.org/flow/520df02be955b412ddb89bcbf70cce34
[{"id":"2d2bd18e.3e4abe","type":"tab","label":"IP Settings","disabled":false,"info":""},{"id":"6a63b433.e2a73c","type":"exec","z":"2d2bd18e.3e4abe","command":"cat /etc/dhcpcd.conf","addpay":false,"append":"","useSpawn":"","timer":"","name":"","x":476,"y":179,"wires":[["3239ea8.73a1016"],[],[]]},{"id":"3239ea8.73a1016","type":"function","z":"2d2bd18e.3e4abe","name":"parse response","func":"var lines = msg.payload.split(\"\\n\");\nvar data = [];\n\nfor(var i=0; i< lines.length; i++){\n if(lines[i].indexOf(\"interface eth0\") != -1)\n break;\n}\n\n//check if interface eth0 is disabled\nif(i < lines.length && lines[i].indexOf(\"#\") >= 0) flow.set('disabled', true);\nelse flow.set('disabled', false);\n\nnode.send({topic:'interface eth0', payload:flow.get('disabled')})\n\ni++;\n\nfor(i; i< lines.length; i++){\n \n //new interface, not eth0\n if(lines[i].indexOf(\"static\") == -1)\n break;\n \n var tmp = lines[i].split(\"=\");\n if(tmp.length==2){\n var prop = tmp[0].split(' ')[1].trim();\n var value = tmp[1].trim();\n \n flow.set(prop, value);\n node.send({topic:prop, payload:value})\n }\n} ","outputs":1,"noerr":0,"x":716,"y":166,"wires":[["3e969ca3.142384"]]},{"id":"3e969ca3.142384","type":"switch","z":"2d2bd18e.3e4abe","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"ip_address","vt":"str"},{"t":"eq","v":"routers","vt":"str"},{"t":"eq","v":"domain_name_servers","vt":"str"},{"t":"eq","v":"interface eth0","vt":"str"}],"checkall":"false","repair":false,"outputs":4,"x":888,"y":166,"wires":[["2d434d1f.145822"],["502308c7.25bef8"],["6a605c6d.329194","140df8ca.dea297"],["ba4f96ca.0f28b8"]]},{"id":"2d434d1f.145822","type":"ui_text_input","z":"2d2bd18e.3e4abe","name":"ip_address","label":"Indirizzo IP","group":"1cf52e38.3cfee2","order":2,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"ip_address","x":1130,"y":120,"wires":[["a825b0e4.2a051"]]},{"id":"502308c7.25bef8","type":"ui_text_input","z":"2d2bd18e.3e4abe","name":"router","label":"Router","group":"1cf52e38.3cfee2","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"routers","x":1109,"y":163,"wires":[["a825b0e4.2a051"]]},{"id":"6a605c6d.329194","type":"ui_text_input","z":"2d2bd18e.3e4abe","name":"dns","label":"DNS","group":"1cf52e38.3cfee2","order":4,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"domain_name_servers","x":1109,"y":206,"wires":[["a825b0e4.2a051"]]},{"id":"9ae462fb.d924d","type":"exec","z":"2d2bd18e.3e4abe","command":"sudo sed -i ","addpay":true,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"","x":510,"y":320,"wires":[[],[],[]]},{"id":"1b55bbd2.2f6a74","type":"ui_button","z":"2d2bd18e.3e4abe","name":"reboot","group":"1cf52e38.3cfee2","order":5,"width":"3","height":"1","passthru":false,"label":"Reboot","color":"","bgcolor":"","icon":"","payload":"By pressing OK the device will be rebooted. If settings are wrong this may cause the device to be unreachable","payloadType":"str","topic":"","x":170,"y":480,"wires":[["846fb983.93f448"]]},{"id":"6da389d0.e8d598","type":"exec","z":"2d2bd18e.3e4abe","command":"sudo reboot","addpay":false,"append":"","useSpawn":"","timer":"","name":"","x":650,"y":480,"wires":[[],[],[]]},{"id":"d0341b01.217ee8","type":"ui_button","z":"2d2bd18e.3e4abe","name":"refresh","group":"1cf52e38.3cfee2","order":6,"width":"3","height":"1","passthru":false,"label":"Update","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"","x":111.44442749023438,"y":247.33291149139404,"wires":[["fb34e190.3926a","c2728ad2.71ac48"]]},{"id":"fb34e190.3926a","type":"delay","z":"2d2bd18e.3e4abe","name":"","pauseType":"delay","timeout":"300","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":278.8888740539551,"y":247.44451141357422,"wires":[["6a63b433.e2a73c"]]},{"id":"c2728ad2.71ac48","type":"function","z":"2d2bd18e.3e4abe","name":"send commands","func":"var properties = flow.get(\"properties\");\n\nvar disabled = flow.get(\"disabled\") ? '#' : '';\n\nif(disabled == '#'){\n //enable interface (replace only first occurance)\n var command = `'0,/#interface eth0/s//interface eth0/g;' /etc/dhcpcd.conf`;\n node.send({payload: command})\n}\n\nfor(var i=0;i<properties.length;i++){\n var p = properties[i];\n \n var old_value = flow.get(p);\n var new_value = flow.get(p+\"_new\") || old_value;\n \n if(old_value && ((old_value != new_value) || disabled == '#'))\n {\n //enable interface (replace only first occurance)\n var command = `'0,\\\\?${disabled}static ${p}=${old_value}?s??static ${p}=${new_value}?g;' /etc/dhcpcd.conf`;\n node.send({payload: command})\n }\n}\n\n","outputs":1,"noerr":0,"x":300,"y":320,"wires":[["8d2915b0.eb5188","9ae462fb.d924d"]]},{"id":"846fb983.93f448","type":"ui_toast","z":"2d2bd18e.3e4abe","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"Cancel","topic":"ATTENTION","name":"","x":330,"y":480,"wires":[["9f141047.ed494"]]},{"id":"9f141047.ed494","type":"switch","z":"2d2bd18e.3e4abe","name":"OK ?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OK","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":480,"wires":[["6da389d0.e8d598"]]},{"id":"a825b0e4.2a051","type":"function","z":"2d2bd18e.3e4abe","name":"store new","func":"flow.set(msg.topic+\"_new\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":1419,"y":161,"wires":[["aef2e472.475238"]]},{"id":"b585fb60.e936a8","type":"inject","z":"2d2bd18e.3e4abe","name":"init","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":122,"y":91,"wires":[["a6810bb0.a3e788"]]},{"id":"a6810bb0.a3e788","type":"function","z":"2d2bd18e.3e4abe","name":"set properties","func":"var properties = ['routers', 'ip_address', 'domain_name_servers'];\n\nflow.set(\"properties\", properties);\n\n//init properties\nfor(var i=0;i<properties.length;i++){\n var p = properties[i];\n flow.set(p, null);\n flow.set(p+\"_new\", null);\n}\n\nnode.send({payload: true});\n","outputs":1,"noerr":0,"x":326,"y":91,"wires":[["6a63b433.e2a73c"]]},{"id":"8d2915b0.eb5188","type":"debug","z":"2d2bd18e.3e4abe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":514,"y":385,"wires":[]},{"id":"fd5e6a37.bc5d38","type":"ui_toast","z":"2d2bd18e.3e4abe","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1310,"y":340,"wires":[]},{"id":"140df8ca.dea297","type":"change","z":"2d2bd18e.3e4abe","name":"set","rules":[{"t":"set","p":"payload","pt":"msg","to":"Settings updated. Reboot device to enable new ip","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1097,"y":310,"wires":[["fd5e6a37.bc5d38"]]},{"id":"ba4f96ca.0f28b8","type":"ui_text","z":"2d2bd18e.3e4abe","group":"1cf52e38.3cfee2","order":1,"width":0,"height":0,"name":"","label":"Static ip","format":"{{!msg.payload}}","layout":"row-spread","x":1120,"y":248,"wires":[]},{"id":"fea93690.140ec8","type":"ui_toast","z":"2d2bd18e.3e4abe","position":"top right","displayTime":"3","highlight":"","outputs":0,"ok":"OK","cancel":"","topic":"","name":"","x":1758,"y":161,"wires":[]},{"id":"aef2e472.475238","type":"change","z":"2d2bd18e.3e4abe","name":"set","rules":[{"t":"set","p":"payload","pt":"msg","to":"New setting stored ","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1566,"y":161,"wires":[["fea93690.140ec8"]]},{"id":"4b72781d.d0e178","type":"ui_text","z":"2d2bd18e.3e4abe","group":"1cf52e38.3cfee2","order":1,"width":0,"height":0,"name":"help","label":"","format":"To store the new value, once edited, press ENTER or TAB","layout":"col-center","x":711,"y":91,"wires":[]},{"id":"1cf52e38.3cfee2","type":"ui_group","z":"","name":"IP Settings","tab":"be53a269.02ec3","disp":true,"width":"6","collapse":false},{"id":"be53a269.02ec3","type":"ui_tab","z":"","name":"Settings","icon":"settings"}]
It looks promising. Looks like it does what I want to do, but I can't get it to work. Couple of things:
What is going on in the send commands function block?
var properties = flow.get("properties");
var disabled = flow.get("disabled") ? '#' : '';
if(disabled == '#'){
//enable interface (replace only first occurance)
var command = `'0,/#interface eth0/s//interface eth0/g;' /etc/dhcpcd.conf`;
node.send({payload: command})
}
for(var i=0;i<properties.length;i++){
var p = properties[i];
var old_value = flow.get(p);
var new_value = flow.get(p+"_new") || old_value;
if(old_value && ((old_value != new_value) || disabled == '#'))
{
//enable interface (replace only first occurance)
var command1 = `'0,\\?${disabled}static ${p}=${old_value}?s??static ${p}=${new_value}?g;' /etc/dhcpcd.conf`;
node.send({payload: command1})
}
}
It passes a payload to the execute block, but I can't see the payload (doesn't show in the debugger node).
Is the sudo sed -i
command correct? I've been reading about the sed -i command and it seems I'm missing something. is the function block before it supposed to feed something as part of the execute command?
I can change the IP address manually by typing in:
nano /etc/dhcpcd.conf
Please advise. Thanks!