Skip to main content

Network API library. A programmatic interface to network devices

Project description

NetAPI

Network API library that perform the core of the work -> requests execution and data parsing.

It relies on the main vendor libraries to perform its work and its main objective to grant network objects that can be used with your applications.

Installation

You can directly install it using pip

pip install netapi

Development Version

You will need poetry for clonning and installing the repo

How it works

Is a library that abstracts the connection and parsing method of the data collected on the target device. It then presents a developer-friendly object that you can use to interact with.

All the objects are created with some cool features for manipulating their attributes and creating your applications on top of it. An example is the IPNetwork object returned of the ipv4 address of an interface. You will see that later.

The library is divided in 3 main types of objects:

  • Device: These are connector objects used fot the net and probes objects. Needed for the execution and data collection.
  • Net: These are network entities, which basically means technologies or parts of the network protocols like interface, vlan, vrrp, bgp, etc...
  • Probes: These are tester objects to run in your device objects. For example ping or trace.

There is also a Metadata object that collects information about the implementation, how many times the object has been executed and timestamp. Useful for reports and other tests.

Network Entities example

Here is an example of how to use the library when using a network entity. In this case Interface:

# Use Arista pyeapi client for connection
from netapi.connector.pyeapier import Device
from netapi.net.pyeapier import Interface

from pprint import pprint

# Create a device connection object using its api https interface
router01 = Device(
    host="192.168.0.77",
    username="someuser",
    password="somepassword",
    transport="https"
)

Show device connection attributes, notice that secrets are hidden

print(router01)

Device(host='192.168.0.77', username='someuser', port=None, net_os='eos', transport='https')

You can see the metadata for this instance and get information like timestamp, type of implementation, id of the instance, among other things:

print(router01.metadata)

Metadata(name='device', type='entity', implementation='EOS-PYEAPI', created_at=datetime.datetime(2019, 5, 14, 16, 47, 35, 319134), id=UUID('51d317ab-584d-4f32-a789-402770c361f2'), updated_at=None, collection_count=0, parent=None)

You can run a command/action and retrieve its output. The format would depend mostly of the device, net_os, implementation (like EOS-PYEAPI or raw SSH for example) and the type of command.

pprint(router01.run('show version'))

{'show version': {'architecture': 'i386',
                  'bootupTimestamp': 1557851831.0,
                  'hardwareRevision': '',
                  'internalBuildId': '1a44ce37-92c4-48b6-b922-38b7eed939b6',
                  'internalVersion': '4.21.5F-11604264.4215F',
                  'isIntlVersion': False,
                  'memFree': 1372616,
                  'memTotal': 2015608,
                  'mfgName': '',
                  'modelName': 'vEOS',
                  'serialNumber': '',
                  'systemMacAddress': '0c:59:30:85:d7:1d',
                  'uptime': 1158.4,
                  'version': '4.21.5F'}}

This library provides common handlers, for well known network technologies. Here is an example for the Management 1 interface. Notice that I pass the device connector object:

mgmt01 = Interface(name="Management1", connector=router01)

# You can collect its attributes by running the get() method
mgmt01.get()

You can see its attributes. Since it is an interface I will only show some key ones

print(mgmt01)

Interface(name='Management1',
          description=None,
          enabled=True,
          status_up=True,
          status='UP',
          last_status_change=datetime.datetime(2019, 5, 14, 17, 38, 45, 217419),
          number_status_changes=4,
          forwarding_model='routed',
          physical=InterfacePhysical(mtu=1500,
              bandwidth=1000000000,
              duplex='duplexFull',
              mac=EUI('0C-59-30-C7-EE-00')),
          optical=InterfaceOptical(tx=None, rx=None, status=None),
          addresses=InterfaceIP(ipv4=IPNetwork('10.193.7.142/24'),
              ipv6=None,
              secondary_ipv4=[],
              dhcp=None),
          instance='default',
          counters=InterfaceCounters(rx_bits_rate=1728.866885299387,
              tx_bits_rate=246.67962344503712,
              rx_pkts_rate=4.007927363735154,
              tx_pkts_rate=0.23434463850156687,
              ...
              tx_errors_tx_pause=0.0),
          members=None,
          last_clear=None,
          counter_refresh=datetime.datetime(2019, 5, 14, 17, 43, 38, 787766), update_interval=300.0)

There are multiple parameters here but and I ommitted othere but you can see that IP addresses are netaddr IPNetwork object and similarly the MAC addresses are EUI objects. Also the counter_refresh interval are datetime objects.

These instances also have metadata

print(mgmt01.metadata)

Metadata(name='interface', type='entity', implementation='EOS-PYEAPI', created_at=datetime.datetime(2019, 5, 14, 16, 43, 44, 819105), id=UUID('73297adf-a0d6-4f8d-b247-0d793e577efb'), updated_at=datetime.datetime(2019, 5, 14, 16, 43, 54, 483277), collection_count=1, parent=None)

You can see that in the metadata the updated_at field is populated? This happened because we invoked the get() method to collect the data. Now let's change the interface description and refresh the interface data (this is done outside of the script)

Assuming a change to the description was done, now retrieve again the data

print(mgmt01.description)

DUMMY DESCRIPTION

You can see that the description changes but also the metadata has been updated!

print(mgmt01.metadata)

Metadata(name='interface', type='entity', implementation='EOS-PYEAPI', created_at=datetime.datetime(2019, 5, 14, 16, 43, 44, 819105), id=UUID('73297adf-a0d6-4f8d-b247-0d793e577efb'), updated_at=datetime.datetime(2019, 5, 14, 17, 20, 27, 557810), collection_count=2, parent=None)

You can see updated_at has been updated :) and the collection_count has increased to 2

Network Probes example

This is a simple example running a Ping against an specific IP address and applying a custom analysis to the result.

First initiate the device object and specify a target

from netapi.connector.linux.subprocesser import Device
from netapi.probe.linux.subprocesser import Ping

host = Device()
ping = Ping(target="1.1.1.1", connector=host)

You can see the Ping object with the following attributes:

print(ping)

Ping(target='1.1.1.1', resolve_target=True, target_ip=IPAddress('1.1.1.1'), target_name='one.one.one.one', source=None, instance=None, count=5, timeout=2, size=692, df_bit=False, interval=1.0, ttl=None, result={})

You can see that result attribute is empty, we need to tell to execute the Ping and we can apply our own logic to it.

The ping object is to be executed on my machine, you can see the command to be used:

print(ping.command())

'ping 1.1.1.1 size 692 repeat 5 timeout 2 interval 1.0'

If we execute it as es we can get a result like

ping.execute()

Ping(target='1.1.1.1', resolve_target=True, target_ip=IPAddress('1.1.1.1'), target_name='one.one.one.one', source=None, instance=None, count=5, timeout=2, size=692, df_bit=False, interval=1.0, ttl=None, result={'probes_sent': 5, 'probes_received': 5, 'packet_loss': 0.0, 'rtt_min': 2.035, 'rtt_avg': 2.073, 'rtt_max': 2.125, 'flag': 'green', 'alert': False, 'status_code': 0, 'status_up': True})

We can use our own threshold in execution to manipulate the flag and status_up parameters.

ping.execute(critical_threshold=-1)

In [17]: ping.result
Out[17]:
{'probes_sent': 5,
 'probes_received': 5,
 'packet_loss': 0.0,
 'rtt_min': 1.929,
 'rtt_avg': 2.209,
 'rtt_max': 2.97,
 'flag': 'red',
 'alert': True,
 'status_code': 2,
 'status_up': False}

You can see that is status_up = False even though all the probes were received successfully. Usefull for custom testing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netapi-0.1.2.tar.gz (28.3 kB view hashes)

Uploaded Source

Built Distribution

netapi-0.1.2-py3-none-any.whl (38.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page