Get local IP Address, mask and MAC from an interface name

This Node-red flow get the ip address, netmask and mac address from a local interface specified by it's name.

Test on Debian 11 Bullseye.

It use "exec" node to run some "ifconfig" commands, the interface name need to be set in "localinterface" attribute, change it in the "inject" node.

The resultant msg contain the following properties :

  • localinterface : set in "inject" node
  • ip_address : the interface ip address
  • netmask : the interface network mask
  • mac_address : the interface MAC address
[{"id":"220e28bf375f67f5","type":"tab","label":"Get IP Address, mask and MAC from an Interface name","disabled":false,"info":"","env":[]},{"id":"1895eaf6ccb469e9","type":"inject","z":"220e28bf375f67f5","name":"set localinterface name","props":[{"p":"localinterface","v":"enp4s0f0","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":180,"wires":[["c4a9aae837d1aed9"]]},{"id":"c4a9aae837d1aed9","type":"template","z":"220e28bf375f67f5","name":"Set commands","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"ifconfig {{localinterface}} | grep '\\<inet\\>' | sed -n '1p' | tr -s ' ' | cut -d ' ' -f3 | cut -d ':' -f2;\nifconfig {{localinterface}} | grep '\\<inet\\>' | sed -n '1p' | tr -s ' ' | cut -d ' ' -f5;\nifconfig {{localinterface}} | grep 'ether' | tr -s ' ' | cut -d ' ' -f3","output":"str","x":360,"y":180,"wires":[["d9edaf5b97fb5711"]]},{"id":"d9edaf5b97fb5711","type":"exec","z":"220e28bf375f67f5","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Get IP","x":510,"y":180,"wires":[["06a0a0fe634f4253"],[],[]]},{"id":"06a0a0fe634f4253","type":"csv","z":"220e28bf375f67f5","name":"","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":false,"include_empty_strings":"","include_null_values":"","x":630,"y":160,"wires":[["2f4dd5a4819c1dfe"]]},{"id":"29463810bee232fc","type":"debug","z":"220e28bf375f67f5","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":160,"wires":[]},{"id":"2f4dd5a4819c1dfe","type":"change","z":"220e28bf375f67f5","name":"","rules":[{"t":"set","p":"ip_address","pt":"msg","to":"payload[0].col1","tot":"msg"},{"t":"set","p":"netmask","pt":"msg","to":"payload[1].col1","tot":"msg"},{"t":"set","p":"mac_address","pt":"msg","to":"payload[2].col1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":160,"wires":[["29463810bee232fc"]]}]

Flow Info

Created 3 years ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • change (x1)
  • csv (x1)
  • debug (x1)
  • exec (x1)
  • inject (x1)
  • template (x1)
Other
  • tab (x1)

Tags

  • network
  • debian
  • ifconfig
  • ip
  • mask
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option