GetHost.Util (GetHost v1.0.1)
View SourceUtility functions for hostname and network operations.
Summary
Functions
Expands an IPv6 address to its full form.
Finds the path to the hostname
executable in the system.
Finds the path to the ping
executable in the system.
Executes a single ping to the specified hostname without DNS resolution.
Gets the short hostname of the system using platform-specific options.
Converts a hostname to its fully qualified form based on the platform.
Functions
Expands an IPv6 address to its full form.
This function handles two types of IPv6 addresses:
- Fully specified addresses (e.g., "2001:db8:1:2:3:4:5:6")
- Compressed addresses with "::" (e.g., "2001:db8::1")
The function expands compressed addresses by:
- Splitting the address at "::"
- Calculating the number of missing segments
- Inserting "0000" for each missing segment
- Padding each segment to 4 characters with leading zeros
Parameters
hostname
- The IPv6 address to expand
Returns
binary()
- The expanded IPv6 address, or the original string if not an IPv6 address
Examples
iex> GetHost.Util.expand_ipv6("2001:db8::1")
"2001:0db8:0000:0000:0000:0000:0000:0001"
iex> GetHost.Util.expand_ipv6("2001:db8:1:2:3:4:5:6")
"2001:0db8:0001:0002:0003:0004:0005:0006"
iex> GetHost.Util.expand_ipv6("not-an-ipv6")
"not-an-ipv6"
Finds the path to the hostname
executable in the system.
Returns
{:ok, path}
- Returns the fullpath
to the hostname executable if found{:error, reason}
- Returns an error tuple with areason
if the executable is not found
Finds the path to the ping
executable in the system.
Returns
{:ok, path}
- Returns the fullpath
to the ping executable if found{:error, reason}
- Returns an error tuple with areason
if the executable is not found
Executes a single ping to the specified hostname without DNS resolution.
This function executes the ping
command with different options based on the operating system:
- On Unix-like systems (macOS, Linux): Uses
-n -c 1
options to prevent DNS resolution and send one packet - On Windows: Uses
/a /n 1
options for address resolution and a single ping
Parameters
hostname
- The hostname or IP address to ping
Returns
{:ok, result}
- Returns the ping command output as a string if successful{:error, reason}
- Returns an error tuple with areason
if the command fails or executable is not found
Gets the short hostname of the system using platform-specific options.
This function executes the hostname
command with different options based on the operating system:
- On macOS (Darwin): Uses
-f
option to get the fully qualified domain name - On Linux: Uses
-i
option to get the IP address - On Windows: Uses default
hostname
command without options
Returns
{:ok, hostname}
- Returns the hostname as a string if successful{:error, reason}
- Returns an error tuple with areason
if the command fails or executable is not found
@spec to_fully_qualified_hostname( {atom(), atom()}, binary() ) :: {:ok, binary()} | {:error, binary()}
Converts a hostname to its fully qualified form based on the platform.
This function handles hostname resolution differently based on the operating system:
- On Unix-like systems (macOS, Linux): Returns the hostname as is
- On Windows: Uses
ping
command to resolve the hostname to its IP address
Parameters
platform
- The platform tuple{:unix, _}
or{:win32, _}
hostname
- The hostname to convert
Returns
{:ok, ip}
- Returns the IP address as a string if successful{:error, reason}
- Returns an error tuple with areason
if the resolution fails