Skip to content

Instantly share code, notes, and snippets.

@elcritch
Created June 25, 2020 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elcritch/d2b6d11856842d8d39d7760d48463c76 to your computer and use it in GitHub Desktop.
Save elcritch/d2b6d11856842d8d39d7760d48463c76 to your computer and use it in GitHub Desktop.
Elixir Module to parse UDHCPD Lease Files
defmodule ParseUdhcpd do
def find_leases(interface \\ "eth0") do
udhcpd_lease_bytes =
"/tmp/vintage_net/udhcpd.#{interface}.leases"
|> File.read!()
<<written_at::signed-integer-64, leases_bytes::bits >> = udhcpd_lease_bytes
parse_leases(leases_bytes, [])
end
def parse_leases("", leases) do
leases
end
def parse_leases(udhcpd_lease_bytes, leases) do
<<expires::signed-integer-32,
ip::4-bytes,
mac::6-bytes,
hostname::20-bytes,
pad::2-bytes,
udhcpd_leases_rest::bits >> = udhcpd_lease_bytes
lease = %{
expires: expires,
ip: ip |> :erlang.binary_to_list() |> :erlang.list_to_tuple(),
hostname: String.trim(hostname,"\0")
}
udhcpd_leases_rest
|> parse_leases(leases++[lease])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment