GetHost.Util (GetHost v1.0.0)

View Source

Utility 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

expand_ipv6(hostname)

@spec expand_ipv6(binary()) :: binary()

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:

  1. Splitting the address at "::"
  2. Calculating the number of missing segments
  3. Inserting "0000" for each missing segment
  4. 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"

hostname_executable()

@spec hostname_executable() :: {:ok, binary()} | {:error, binary()}

Finds the path to the hostname executable in the system.

Returns

  • {:ok, path} - Returns the full path to the hostname executable if found
  • {:error, reason} - Returns an error tuple with a reason if the executable is not found

ping_executable()

@spec ping_executable() :: {:ok, binary()} | {:error, binary()}

Finds the path to the ping executable in the system.

Returns

  • {:ok, path} - Returns the full path to the ping executable if found
  • {:error, reason} - Returns an error tuple with a reason if the executable is not found

ping_raw_oneshot(hostname)

@spec ping_raw_oneshot(binary()) :: {:ok, binary()} | {:error, binary()}

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 a reason if the command fails or executable is not found

short_name()

@spec short_name() :: {:ok, binary()} | {:error, binary()}

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 a reason if the command fails or executable is not found

to_fully_qualified_hostname(arg, hostname)

@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 a reason if the resolution fails