Last active
October 18, 2018 08:33
-
-
Save houaq/d2eced4d38434efd37304fa8468f051c to your computer and use it in GitHub Desktop.
Linux networking config file (/etc/network/interfaces) parser by Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function rtrim(s) | |
| local x = string.find(s, '%s+$') | |
| if x then | |
| return string.sub(s, 1, x - 1) | |
| else | |
| return s | |
| end | |
| end | |
| local function parseIfs() | |
| local autos = {} | |
| local ifaces = {} | |
| local iface | |
| local fp = io.open('/etc/network/interfaces', 'r') | |
| while true do | |
| local line = fp:read() | |
| if not line then break end | |
| line = string.gsub(line, '%s*#.*', '') | |
| if string.find(line, '^auto') then | |
| local _,_,x = string.find(line, 'auto%s+(%w+)') | |
| autos[x] = true | |
| elseif string.find(line, '^iface') then | |
| local _,_,name,proto = string.find(line, 'iface%s+(%w+)%s+inet%s+(%w+)') | |
| iface = { ifname = name, proto = proto, auto = autos[name] } | |
| ifaces[name] = iface | |
| else | |
| if iface then | |
| local _,_,k,v = string.find(line, '%s*(%a+)%s+(%w.*)') | |
| if k and v then iface[k] = rtrim(v) end | |
| end | |
| end | |
| end | |
| fp:close() | |
| return ifaces | |
| end | |
| local ifaces = parseIfs() | |
| -- ifaces: [[ | |
| { | |
| "atml0": { | |
| "proto": "dhcp", | |
| "ifname": "atml0" | |
| }, | |
| "wlan0": { | |
| "essid": "any", | |
| "proto": "dhcp", | |
| "mode": "managed", | |
| "driver": "wext", | |
| "ifname": "wlan0" | |
| }, | |
| "usb0": { | |
| "proto": "dhcp", | |
| "ifname": "usb0" | |
| }, | |
| "tiwlan0": { | |
| "essid": "any", | |
| "ifname": "tiwlan0", | |
| "mode": "managed", | |
| "proto": "dhcp" | |
| }, | |
| "lo": { | |
| "ifname": "lo", | |
| "auto": true, | |
| "proto": "loopback" | |
| }, | |
| "eth0": { | |
| "ifname": "eth0", | |
| "auto": true, | |
| "proto": "dhcp" | |
| }, | |
| "eth1": { | |
| "proto": "static", | |
| "auto": true, | |
| "address": "10.10.80.15", | |
| "gateway": "10.10.80.1", | |
| "netmask": "255.0.0.0", | |
| "ifname": "eth1" | |
| } | |
| } | |
| ]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment