github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/sockaddr.md (about)

     1  ---
     2  title: sockaddr functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  This namespace wraps the [`github.com/hashicorp/go-sockaddr`](https://github.com/hashicorp/go-sockaddr)
     9  package, which makes it easy to discover information about a system's network
    10  interfaces.
    11  
    12  These functions are _partly_ documented here for convenience, but the canonical
    13  documentation is at https://godoc.org/github.com/hashicorp/go-sockaddr.
    14  
    15  Aside from some convenience functions, the general method of working with these
    16  functions is through a _pipeline_. There are _source_ functions, which select
    17  interfaces ([`IfAddr`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfAddr)),
    18  and there are functions to further filter, refine, and finally to select
    19  the specific attributes you're interested in.
    20  
    21  To demonstrate how this can be used, here's an example that lists all of the IPv4 addresses available on the system:
    22  
    23  _in.tmpl:_
    24  ```
    25  {{ range (sockaddr.GetAllInterfaces | sockaddr.Include "type" "ipv4") -}}
    26  {{ . | sockaddr.Attr "address" }}
    27  {{end}}
    28  ```
    29  
    30  ```console
    31  $ gomplate -f in.tmpl
    32  127.0.0.1
    33  10.0.0.8
    34  132.79.79.79
    35  ```
    36  
    37  [RFC 1918]: http://tools.ietf.org/html/rfc1918
    38  [RFC 6890]: http://tools.ietf.org/html/rfc6890
    39  
    40  ## `sockaddr.GetAllInterfaces`
    41  
    42  Iterates over all available network interfaces and finds all available IP
    43  addresses on each interface and converts them to `sockaddr.IPAddrs`, and returning
    44  the result as an array of `IfAddr`.
    45  
    46  Should be piped through a further function to refine and extract attributes.
    47  
    48  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
    49  ### Usage
    50  
    51  ```
    52  sockaddr.GetAllInterfaces
    53  ```
    54  
    55  
    56  ## `sockaddr.GetDefaultInterfaces`
    57  
    58  Returns `IfAddrs` of the addresses attached to the default route.
    59  
    60  Should be piped through a further function to refine and extract attributes.
    61  
    62  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
    63  ### Usage
    64  
    65  ```
    66  sockaddr.GetDefaultInterfaces
    67  ```
    68  
    69  
    70  ## `sockaddr.GetPrivateInterfaces`
    71  
    72  Returns an array of `IfAddr`s containing every IP that matches
    73  [RFC 6890][], is attached to the interface with
    74  the default route, and is a forwardable IP address.
    75  
    76  **Note:** [RFC 6890][] is a more exhaustive version of [RFC 1918][]
    77  because it spans IPv4 and IPv6, however it does permit the inclusion of likely
    78  undesired addresses such as multicast, therefore our definition of a "private"
    79  address also excludes non-forwardable IP addresses (as defined by the IETF).
    80  
    81  Should be piped through a further function to refine and extract attributes.
    82  
    83  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
    84  ### Usage
    85  
    86  ```
    87  sockaddr.GetPrivateInterfaces
    88  ```
    89  
    90  
    91  ## `sockaddr.GetPublicInterfaces`
    92  
    93  Returns an array of `IfAddr`s that do not match [RFC 6890][],
    94  are attached to the default route, and are forwardable.
    95  
    96  Should be piped through a further function to refine and extract attributes.
    97  
    98  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
    99  ### Usage
   100  
   101  ```
   102  sockaddr.GetPublicInterfaces
   103  ```
   104  
   105  
   106  ## `sockaddr.Sort`
   107  
   108  Returns an array of `IfAddr`s sorted based on the given selector. Multiple sort
   109  clauses can be passed in as a comma-delimited list without whitespace.
   110  
   111  ### Selectors
   112  
   113  The valid selectors are:
   114  
   115  | selector | sorts by... |
   116  |----------|-------------|
   117  | `address` | the network address |
   118  | `default` | whether or not the `IfAddr` has a default route |
   119  | `name` | the interface name |
   120  | `port` | the port, if included in the `IfAddr` |
   121  | `size` | the size of the network mask, smaller mask (larger number of hosts per network) to largest (e.g. a /24 sorts before a /32) |
   122  | `type` | the type of the `IfAddr`. Order is Unix, IPv4, then IPv6 |
   123  
   124  Each of these selectors sort _ascending_, but a _descending_ sort may be chosen
   125  by prefixing the selector with a `-` (e.g. `-address`). You may prefix with a `+`
   126  to make explicit that the sort is ascending.
   127  
   128  `IfAddr`s that are not comparable will be at the end of the list and in a
   129  non-deterministic order.
   130  
   131  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   132  ### Usage
   133  
   134  ```
   135  sockaddr.Sort selector <array-of-IfAddrs>
   136  ```
   137  ```
   138  <array-of-IfAddrs> | sockaddr.Sort selector
   139  ```
   140  
   141  ### Arguments
   142  
   143  | name | description |
   144  |------|-------------|
   145  | `selector` | _(required)_ which selector to use (see above for values) |
   146  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s to sort |
   147  
   148  ### Examples
   149  
   150  To sort first by interface name, then by address (descending):
   151  ```console
   152  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Sort "name,-address" }}'
   153  ```
   154  
   155  ## `sockaddr.Exclude`
   156  
   157  Returns an array of `IfAddr`s filtered by interfaces that do not match the given
   158  selector's value.
   159  
   160  ### Selectors
   161  
   162  The valid selectors are:
   163  
   164  | selector | excludes by... |
   165  |----------|-------------|
   166  | `address` | the network address |
   167  | `flag` | the specified flags (see below) |
   168  | `name` | the interface name |
   169  | `network` | being part of the given IP network (in net/mask format) |
   170  | `port` | the port, if included in the `IfAddr` |
   171  | `rfc` | being included in networks defined by the given RFC. See [the source code](https://github.com/hashicorp/go-sockaddr/blob/master/rfc.go#L38) for a list of valid RFCs |
   172  | `size` | the size of the network mask, as number of bits (e.g. `"24"` for a /24) |
   173  | `type` | the type of the `IfAddr`. `unix`, `ipv4`, or `ipv6` |
   174  
   175  #### supported flags
   176  
   177  These flags are supported by the `flag` selector:
   178  `broadcast`, `down`, `forwardable`, `global unicast`, `interface-local multicast`,
   179  `link-local multicast`, `link-local unicast`, `loopback`, `multicast`, `point-to-point`,
   180  `unspecified`, `up`
   181  
   182  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   183  ### Usage
   184  
   185  ```
   186  sockaddr.Exclude selector value <array-of-IfAddrs>
   187  ```
   188  ```
   189  <array-of-IfAddrs> | sockaddr.Exclude selector value
   190  ```
   191  
   192  ### Arguments
   193  
   194  | name | description |
   195  |------|-------------|
   196  | `selector` | _(required)_ which selector to use (see above for values) |
   197  | `value` | _(required)_ the selector value to exclude |
   198  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s to consider |
   199  
   200  ### Examples
   201  
   202  To exclude all IPv6 interfaces:
   203  ```console
   204  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Exclude "type" "ipv6" }}'
   205  ```
   206  
   207  ## `sockaddr.Include`
   208  
   209  Returns an array of `IfAddr`s filtered by interfaces that match the given
   210  selector's value.
   211  
   212  This is the inverse of `sockaddr.Exclude`. See [`sockaddr.Exclude`](#sockaddr.Exclude) for details.
   213  
   214  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   215  ### Usage
   216  
   217  ```
   218  sockaddr.Include selector value <array-of-IfAddrs>
   219  ```
   220  ```
   221  <array-of-IfAddrs> | sockaddr.Include selector value
   222  ```
   223  
   224  ### Arguments
   225  
   226  | name | description |
   227  |------|-------------|
   228  | `selector` | _(required)_ which selector to use (see above for values) |
   229  | `value` | _(required)_ the selector value to include |
   230  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s to consider |
   231  
   232  ## `sockaddr.Attr`
   233  
   234  Returns the named attribute as a string.
   235  
   236  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   237  ### Usage
   238  
   239  ```
   240  sockaddr.Attr selector <array-of-IfAddrs>
   241  ```
   242  ```
   243  <array-of-IfAddrs> | sockaddr.Attr selector
   244  ```
   245  
   246  ### Arguments
   247  
   248  | name | description |
   249  |------|-------------|
   250  | `selector` | _(required)_ the attribute to return |
   251  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s to inspect |
   252  
   253  ### Examples
   254  
   255  ```console
   256  $ gomplate -i '{{ range (sockaddr.GetAllInterfaces | sockaddr.Include "type" "ipv4") }}{{ . | sockaddr.Attr "name" }} {{end}}'
   257  lo0 en0
   258  ```
   259  
   260  ## `sockaddr.Join`
   261  
   262  Selects the given attribute from each `IfAddr` in the source array, and joins
   263  the results with the given separator.
   264  
   265  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   266  ### Usage
   267  
   268  ```
   269  sockaddr.Join selector separator <array-of-IfAddrs>
   270  ```
   271  ```
   272  <array-of-IfAddrs> | sockaddr.Join selector separator
   273  ```
   274  
   275  ### Arguments
   276  
   277  | name | description |
   278  |------|-------------|
   279  | `selector` | _(required)_ the attribute to select |
   280  | `separator` | _(required)_ the separator |
   281  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s to join |
   282  
   283  ### Examples
   284  
   285  ```console
   286  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Join "name" "," }}'
   287  lo0,lo0,lo0,en0,en0
   288  ```
   289  
   290  ## `sockaddr.Limit`
   291  
   292  Returns a slice of `IfAddr`s based on the specified limit.
   293  
   294  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   295  ### Usage
   296  
   297  ```
   298  sockaddr.Limit limit <array-of-IfAddrs>
   299  ```
   300  ```
   301  <array-of-IfAddrs> | sockaddr.Limit limit
   302  ```
   303  
   304  ### Arguments
   305  
   306  | name | description |
   307  |------|-------------|
   308  | `limit` | _(required)_ the maximum number of `IfAddrs` |
   309  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s |
   310  
   311  ### Examples
   312  
   313  ```console
   314  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Limit 2 | sockaddr.Join "name" "|" }}'
   315  lo0|lo0
   316  ```
   317  
   318  ## `sockaddr.Offset`
   319  
   320  Returns a slice of `IfAddr`s based on the specified offset.
   321  
   322  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   323  ### Usage
   324  
   325  ```
   326  sockaddr.Offset offset <array-of-IfAddrs>
   327  ```
   328  ```
   329  <array-of-IfAddrs> | sockaddr.Offset offset
   330  ```
   331  
   332  ### Arguments
   333  
   334  | name | description |
   335  |------|-------------|
   336  | `offset` | _(required)_ the offset |
   337  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s |
   338  
   339  ### Examples
   340  
   341  ```console
   342  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Limit 2 | sockaddr.Offset 1 | sockaddr.Attr "address" }}'
   343  ::1
   344  ```
   345  
   346  ## `sockaddr.Unique`
   347  
   348  Creates a unique array of `IfAddr`s based on the matching selector. Assumes the input has
   349  already been sorted.
   350  
   351  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   352  ### Usage
   353  
   354  ```
   355  sockaddr.Unique selector <array-of-IfAddrs>
   356  ```
   357  ```
   358  <array-of-IfAddrs> | sockaddr.Unique selector
   359  ```
   360  
   361  ### Arguments
   362  
   363  | name | description |
   364  |------|-------------|
   365  | `selector` | _(required)_ the attribute to select |
   366  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s |
   367  
   368  ### Examples
   369  
   370  ```console
   371  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Unique "name" | sockaddr.Join "name" ", " }}'
   372  lo0, en0
   373  ```
   374  
   375  ## `sockaddr.Math`
   376  
   377  Applies a math operation to each `IfAddr` in the input. Any failure will result in zero results.
   378  
   379  See [the source code](https://github.com/hashicorp/go-sockaddr/blob/master/ifaddrs.go#L725)
   380  for details.
   381  
   382  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   383  ### Usage
   384  
   385  ```
   386  sockaddr.Math selector operation <array-of-IfAddrs>
   387  ```
   388  ```
   389  <array-of-IfAddrs> | sockaddr.Math selector operation
   390  ```
   391  
   392  ### Arguments
   393  
   394  | name | description |
   395  |------|-------------|
   396  | `selector` | _(required)_ the attribute to operate on |
   397  | `operation` | _(required)_ the operation |
   398  | `<array-of-IfAddrs>` | _(required)_ the array of `IfAddr`s |
   399  
   400  ### Examples
   401  
   402  ```console
   403  $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Math "address" "+5" | sockaddr.Attr "address" }}'
   404  127.0.0.6
   405  ```
   406  
   407  ## `sockaddr.GetPrivateIP`
   408  
   409  Returns a string with a single IP address that is part of [RFC 6890][] and has a
   410  default route. If the system can't determine its IP address or find an [RFC 6890][]
   411  IP address, an empty string will be returned instead.
   412  
   413  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   414  ### Usage
   415  
   416  ```
   417  sockaddr.GetPrivateIP
   418  ```
   419  
   420  
   421  ### Examples
   422  
   423  ```console
   424  $ gomplate -i '{{ sockaddr.GetPrivateIP }}'
   425  10.0.0.28
   426  ```
   427  
   428  ## `sockaddr.GetPrivateIPs`
   429  
   430  Returns a space-separated string with all IP addresses that are part of [RFC 6890][]
   431  (regardless of whether or not there is a default route, unlike `GetPublicIP`).
   432  If the system can't find any [RFC 6890][] IP addresses, an empty string will be
   433  returned instead.
   434  
   435  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   436  ### Usage
   437  
   438  ```
   439  sockaddr.GetPrivateIPs
   440  ```
   441  
   442  
   443  ### Examples
   444  
   445  ```console
   446  $ gomplate -i '{{ sockaddr.GetPrivateIPs }}'
   447  10.0.0.28 192.168.0.1
   448  ```
   449  
   450  ## `sockaddr.GetPublicIP`
   451  
   452  Returns a string with a single IP address that is NOT part of [RFC 6890][] and
   453  has a default route. If the system can't determine its IP address or find a
   454  non-[RFC 6890][] IP address, an empty string will be returned instead.
   455  
   456  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   457  ### Usage
   458  
   459  ```
   460  sockaddr.GetPublicIP
   461  ```
   462  
   463  
   464  ### Examples
   465  
   466  ```console
   467  $ gomplate -i '{{ sockaddr.GetPublicIP }}'
   468  8.1.2.3
   469  ```
   470  
   471  ## `sockaddr.GetPublicIPs`
   472  
   473  Returns a space-separated string with all IP addresses that are NOT part of
   474  [RFC 6890][] (regardless of whether or not there is a default route, unlike
   475  `GetPublicIP`). If the system can't find any non-[RFC 6890][] IP addresses, an
   476  empty string will be returned instead.
   477  
   478  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   479  ### Usage
   480  
   481  ```
   482  sockaddr.GetPublicIPs
   483  ```
   484  
   485  
   486  ### Examples
   487  
   488  ```console
   489  $ gomplate -i '{{ sockaddr.GetPublicIPs }}'
   490  8.1.2.3 8.2.3.4
   491  ```
   492  
   493  ## `sockaddr.GetInterfaceIP`
   494  
   495  Returns a string with a single IP address sorted by the size of the network
   496  (i.e. IP addresses with a smaller netmask, larger network size, are sorted first).
   497  
   498  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   499  ### Usage
   500  
   501  ```
   502  sockaddr.GetInterfaceIP name
   503  ```
   504  
   505  ### Arguments
   506  
   507  | name | description |
   508  |------|-------------|
   509  | `name` | _(required)_ the interface name |
   510  
   511  ### Examples
   512  
   513  ```console
   514  $ gomplate -i '{{ sockaddr.GetInterfaceIP "en0" }}'
   515  10.0.0.28
   516  ```
   517  
   518  ## `sockaddr.GetInterfaceIPs`
   519  
   520  Returns a string with all IPs, sorted by the size of the network (i.e. IP
   521  addresses with a smaller netmask, larger network size, are sorted first), on a
   522  named interface.
   523  
   524  _Added in gomplate [v2.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.4.0)_
   525  ### Usage
   526  
   527  ```
   528  sockaddr.GetInterfaceIPs name
   529  ```
   530  
   531  ### Arguments
   532  
   533  | name | description |
   534  |------|-------------|
   535  | `name` | _(required)_ the interface name |
   536  
   537  ### Examples
   538  
   539  ```console
   540  $ gomplate -i '{{ sockaddr.GetInterfaceIPs "en0" }}'
   541  10.0.0.28 fe80::1f9a:5582:4b41:bd18
   542  ```