Skip to content

Working with IP addresses in Dataflows

There are a number of built-in filters for working with IP addresses and networks.

These build off of the ipaddress module from python which contains a number of useful functions. See the introduction to the ipaddress module as a starting point.

Testing for IP addresses

A convenience filter is_ip() can be used to test if a string is a valid IP addresses or not. If non-valid IP address strings are used with the IP functions, a ValueError will be raised and cause the dataflow to fail. To avoid this, is_ip() can be used as a test.

For example, consider the following failing dataflow:

variables:
    ip_str: "BADIPSTRING"

steps:
  - name: step_triggers_exception
    params:
        output: |
          {{ ip_str|ip_interface }}

This can be corrected by adding a test before converting the string to an IP address:

variables:
    ip_str: "BADIPSTRING"

steps:
  - name: step_passes_with_no_error
    params:
        output: |
          {{ ip_str|ip_interface if ip_string|is_ip }}

Note that this produces no output so even through the error is cleared, consideration should still be given on what to do with bad IP address strings.

variables:
    ip_strs:
      - "BADIPSTRING"
      - "10.1.2.3"

steps:
  - name: test_ip_strings
    params:
        output: |
          {% for s in ip_strs %}
            {{ s }} is {{ '' if is_ip(s) else 'not ' }}a valid IP address.
          {% endfor %}

Converting strings into IP addresses and networks

The following IP jinja filters are available to convert strings into addresses and networks:

  • ip_interface()
  • ip_address()
  • ip_network()

See the rvrdata filter reference for a full list of available custom filters.

Once converted, any of the object methods or operators can be used in jinja expressions. For a detailed list, see the python ipaddress module.

Additional IP address filters

The following additional filters are available:

  • is_subnet_of() to check if a CIDR block is the child of another CIDR block.
  • first_ip_address() to get the first IP address of a CIDR block.
  • netmask_to_cidr() to get a CIDR block shorthand version of a network and netmask.