Current section

Files

Jump to
wifi_password lib wifi_password.ex
Raw

lib/wifi_password.ex

defmodule WifiPassword do
@moduledoc """
Documentation for `WifiPassword`.
"""
@spec get(binary) :: {:error, :denied | :not_found} | {:ok, binary}
@doc """
Returns the password of your a given router based on its ssid
## Examples
iex> WifiPassword.get("my-router")
{:ok, "YoUrWiFiPasSwOrD"}
iex> WifiPassword.get("my-invalid-router")
{:error, :not_found}
"""
def get(ssid) do
os = :os.type()
case os do
{:unix, :darwin} ->
case System.cmd("security", [
"find-generic-password",
"-wa",
ssid
]) do
{_any, 128} ->
{:error, :denied}
{_any, 44} ->
{:error, :not_found}
{passwd, 0} ->
{:ok, passwd |> String.replace("\n", "")}
end
{:win32, :nt} ->
{:error, :not_implemented}
end
end
end