Skip to content

Instantly share code, notes, and snippets.

@robertsLando
Last active Jul 21, 2021
Embed
What would you like to do?
Raspberry PI eth0 static IP network configuration via Dashboard

This flows allows users to manually change and enable the static IP configuration of the Raspberry PI.

This flow reads configuration in /etc/dhcpcd.conf file and uses sed commands to enable static ip on eth0 interface end manually set the new static ip parameters.

If you have never edited the /etc/dhcpcd.conf file this flow will work without any problem, it expect to find something like this

#interface eth0
#static ip_address=192.168.1.xx/24
#static routers=192.168.1.xx
#static domain_name_servers=192.168.1.xx

or this

interface eth0
static ip_address=192.168.1.xx/24
static routers=192.168.1.xx
static domain_name_servers=192.168.1.xx

This flow reads the entire content of the /etc/dhcpcd.conf file and starts reading line by line. Once he find the line interface eth0 or #interface eth0 it starts to store actual configuration values until he finds a line that doesn't contain static. For this reason DON'T ADD BLANK LINES OR COMMENTS to the configuration file or this flow will not work as expected

The flow will edit this file using sed so be careful and make sure you set a valid configuration before reboot your Raspberry.

Buy Me A Coffee

Enjoy

[{"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"}]
@ckwiek

This comment has been minimized.

Copy link

@ckwiek ckwiek commented Oct 30, 2017

Hello,
I trying to use your flow but I dont´t know why dont´t work, the UI show the IP, the button restart work fine, but if I change and restart for an new IP nothing happen, the IP don´t change, some ideia what is wrong? Thank you!

@robertsLando

This comment has been minimized.

Copy link
Owner Author

@robertsLando robertsLando commented Sep 6, 2018

@ckwiek Updated

@roblahnst

This comment has been minimized.

Copy link

@roblahnst roblahnst commented Mar 14, 2021

It would be nice to have a function that sets it back to DHCP.

@awneil

This comment has been minimized.

Copy link

@awneil awneil commented Jul 21, 2021

The 'Update' doesn't work; the Node-RED console shows the following:

pam_unix(sudo:auth): conversation failed
pam_unix(sudo:auth): auth could not identify password for [pi]

image

Similarly for the 'Reboot'.

What to do?

EDIT

The Flow seems to rely on issuing 'sudo' commands - which is not allowed: https://discourse.nodered.org/t/sudo-in-exec-node/4762

@awneil

This comment has been minimized.

Copy link

@awneil awneil commented Jul 21, 2021

The Flow seems to rely on issuing 'sudo' commands - which is not allowed

Adding a Debug node to the stderr out of the "sudo sed -i" Exec node:

image

Gives:

image

ie,

sudo: no tty present and no askpass program specified
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment