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