Tag: github

  • j2ipaddr

    Jinja2 filters for IP addresses, the easy way

    Why

    On networking and network automation, we need to extract info about IP addresses as a combination of two values:

    • a host address
    • a subnet mask

    For 10.10.10.5/24, the host address is 10.10.10.5 and the subnet mask is 255.255.255.0, and its prefix length is 24.

    There is additional information we can infer from this single item, as its network address, broadcast address.

    Useful data for network engineers are wildcards or hostmasks, network size, class, type, and so on.

    Jinja2 provides several integrated filters to work with, however it can be complicated to use complex data types.

    Ansible provides a way to work this on its ansible.utils.ipaddr collection.

    However, probably you won’t need the entire Ansible package just to be able to use it.

    This package intends to provide a set of filters and handler to the Python 3 netaddr module, on a way that is hopefully easy and lightweight to use.

    What

    Included filters are the following:

    ip_address(addr)

    Returns an IP address for a combination of IP address and subnet mask

    ip_address('10.10.10.5/24')
    > 10.10.10.5
    {{ '10.10.10.5/24 | ip_address }}
    > 10.10.10.5

    ip_prefixlen(addr)

    Returns a prefix length for a combination of IP address and subnet mask

    ip_prefixlen('10.10.10.5/24')
    > 24
    {{ '10.10.10.5/24 | ip_prefixlen }}
    > 24

    ip_netmask(addr)

    Returns a subnet mask for a combination of IP address and subnet mask

    ip_netmask('10.10.10.5/24')
    > 255.255.255.0
    {{ '10.10.10.5/24 | ip_netmask }}
    > 255.255.255.0

    ip_hostmask(addr)

    Returns a wilcard or hostmask for a combination of IP address and subnet mask

    ip_hostmask('10.10.10.5/24')
    > 0.0.0.255
    {{ '10.10.10.5/24 | ip_hostmask }}
    > 0.0.0.255

    ip_wildcard(addr)

    Alias for ip_hostmask(addr)

    ip_wildcard('10.10.10.5/24')
    > 0.0.0.255
    {{ '10.10.10.5/24 | ip_wildcard }}
    > 0.0.0.255

    ip_network(addr)

    Returns a network address for a combination of IP address and subnet mask

    ip_network('10.10.10.5/24')
    > 10.10.10.0
    {{ '10.10.10.5/24 | ip_network_hosts_size }}
    > 10.10.10.0

    ip_broadcast(addr)

    Returns a broadcast address for a combination of IP address and subnet mask

    ip_broadcast('10.10.10.5/24')
    > 10.10.10.255
    {{ '10.10.10.5/24 | ip_broadcast }}
    > 10.10.10.255

    ip_network_hosts_size(addr)

    Returns the size of the subnet for a combination of IP address and subnet mask

    ip_network_hosts_size('10.10.10.5/24')
    > 255
    {{ '10.10.10.5/24 | ip_network_hosts_size }}
    > 255

    ip_network_first(addr)

    Returns the first usable address in network address for a combination of IP address and subnet mask

    ip_network('10.10.10.5/24')
    > 10.10.10.1
    {{ '10.10.10.5/24 | ip_network_hosts_size }}
    > 10.10.10.1

    ip_network_last(addr)

    Returns the last usable address in network address for a combination of IP address and subnet mask

    ip_network('10.10.10.5/24')
    > 10.10.10.254
    {{ '10.10.10.5/24 | ip_network_hosts_size }}
    > 10.10.10.254

    How

    Simply install with pip.

    $ pip install j2ipaddr

    To insert the filters on your Jinja2 processor, simply use the following syntax. The filter name can be changed by adjusting the dict key name.

    import jinja2
    import j2ipaddr.filters
    jinja2.filters.FILTERS['ip_prefixlen'] = filters.ip_prefixlen

    Or, probably an easier way, use the following one-liner to load all the filters into your Jinja2 filters

    import jinja2
    import j2ipaddr.filters
    jinja2.filters.FILTERS = {**jinja2.filters.FILTERS, **filters.load_all()}

    On your templates, you can do this as an example:

    Variables

    host:
      interfaces:
        Te1/0/1:
          ipv4_addresses:
            - 10.10.10.5/24

    Template

    router ospf 10
      network {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_network }} {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_wildcard  }} area 0.0.0.0

    The output would looks like this:

    router ospf 10
      network 10.0.0.0 0.0.0.255 area 0.0.0.0

    Where

    You can find this project on

  • UFiber v4 Python Client

    Yeah, I finally remembered to make a post about this. I know it will like as a copy-paste of the previous one, because, in fact it is.

    Ok, if you have been following the series, you should already know that I equally love and hate UFiber OLTs. They are affordable, deliver a lot of bang for the buck, and have an awful GUI.

    Well, the GUI is lovely on v4.

    Python in the middle

    I wrote a quick and dirty client which acts as a sort of middleware between the HTTP inteface of the OLT and you.

    It allows to provision non existing ONUs, GPON profiles, WiFi profiles, retrieve active ONU status and general configuration.

    Take a look to it on https://github.com/baldoarturo/ufiber-client-4, and feel free to contribute if you want to.

    How to help

    It would be awesome to have docs 😀

    Are you a pydoc master? Let’a add docstrings.

    Do you have an OLT for me to test? Ping me and we can set up a VPN.

    olt.py

    This is the core of the project. It uses the OLTClient class to provide a middleware between you and the HTTP interface of the OLT.

    Initialize a new OLTClient instance with:

    client = OLTClient(host='192.168.1.1', username='ubnt', password='ubnt', debug_level=logging.DEBUG)

    Required params are only host, and credentials.

    The initialization will handle the login for you, altough you can call the login() method manually.

    If the OLT is network reachable, and you have provided the right credentials, and the OLT GUI is alive and well, you should be ready to start.

    What changes on v4

    Well, UBNT got rid of the GPON profiles. 🙁

    This software is intented to give you an alternative by keeping profiles as JSON in the ./profiles folder.

    You can copy the template.json file and make your way using it as a starting point. It should be self descriptive.

    There is an schema.json which validates your profile before pushing changes into the OLT.

  • UFiber Python Client

    Ok, if you have been following the series, you should already know that I equally love and hate UFiber OLTs. They are affordable, deliver a lot of bang for the buck, and have an awful GUI.

    Please, be aware that this can change for better or worse in the future, and at the time I’m writing this the latest firmware is v3.1.3. I trust in you UBNT, hope you can sort out this and give us a better product. I’ll keep my fingers crossed.

    Python in the middle

    I wrote a quick and dirty client which acts as a sort of middleware between the HTTP inteface of the OLT and you.

    It allows to provision non existing ONUs, GPON profiles, WiFi profiles, retrieve active ONU status and general configuration.

    Take a look to it on https://github.com/baldoarturo/ufiber-client, and feel free to contribute if you want to.

    Edited on Aug 15 2020: I did the same for firmware version 4, which is cleaner and fixes a lot of bugs. Stay tuned!

    ufiber-client

    This is a quick dirty project built to provide a quick dirty client for Ubiquiti UFiber OLTs, using firmware version 3.x

    There is also a CLI attempt, but I couldn’t find any ready to use packages to build a decent CLI.

    More info about what am I doing this is on the following entries:

    olt.py

    This is the core of the project. It uses the OLTCLient class to provide a middleware between you and the HTTP interface of the olt.

    Initialize a new OLTClient instance with:

    client = olt.OLTClient(host, username, password)

    The initialization will handle the login for you, altough you can call the login() method manually.

    If the OLT is network reacheable, and you have provided the right credentials, and the OLT WEB GUI is alive and well, you should be ready to start.

    You can also connect using cli.py:

    $ /cli.py
    UFiber Client for fw version 3.1.3
    UFiber> help
    
    Documented commands (type help <topic>):
    ========================================
    connect  help  onu  quit  show
    
    UFiber> connect 10.20.0.101
    Username:admin
    Password:
    Logging to 10.20.0.101 ...
    Connection OK
    UFiber>
  • Contributing to Netbox devices library

    The Netbox community has launched a repository for standard devices.

    https://github.com/netbox-community/devicetype-library

    This comes handy for new and existing Netbox installations, because now you can populate your database with predefined device models for the most common networking gear manufacturers.

    I have contributed to the repository and created the entire set of MikroTik routers and switches, updated to December 2019.

    https://github.com/netbox-community/devicetype-library/tree/master/device-types/MikroTik

    Feel free to use it and contribute!
    And as always, you can find me on Github at https://github.com/baldoarturo