Python wrapper for Netdot Web API.
Project description
A python wrapper for UO-NetDot's RESTful API.
⚠ Disclaimer: From 0.2.0 onward, this API wrapper does not ensure support for the de facto Open Source version of NetDot (GitHub).
Install
This package is deployed to pypi.org.
Download it with pip
:
pip install netdot
Interactive Usage (Python interpreter)
ℹ Many methods of
netdot.Repository
andnetdot.dataclasses
are actually runtime-generated code. So, using the Repository interactively at the interpreter allows using features like tab completion and 'help()
' function (discussed in Appendix below) to learn more.
Before getting into building a massive integration/tool, you might jump in and get some experience. Thankfully, we have the Python interpreter where we can jump in and do some testing!
# Enter the Python interpreter by running just "python" in your shell
$ python
Python 3.6.15 (default, Sep 10 2021, 00:26:58)
... omitted for brevity...
>>> import netdot
>>>
ℹ The Python interpreter is often referred to as 'a REPL' (Read-Eval-Print-Loop). If you are unfamiliar with the Python interpreter, aka 'REPL', you might want to get started by reading "Using the REPL (in VSCode)" documentation.
With the netdot package imported, you can proceed with setting up a connecting and downloading some data!
Connecting in the interpreter: netdot.connect()
We have enabled interpreter-usage as a first-class feature.
In particular, you will want to use the connect
function like the following.
netdot.connect()
returns anetdot.Repository
instance.
>>> import netdot
>>> repo = netdot.connect()
What is the URL of the NetDot server? [https://nsdb.uoregon.edu]: ('enter' to use default)
NetDot username: myusername
NetDot password: ********** (using getpass module, to securely collect password)
That repo
can be used for some interactive viewing of NetDot's data.
Example: Lookup IP Address in interpreter
As an example, you can use this API to lookup an IP Address.
>>> ipaddr = repo.get_ipblock_by_address('128.223.61.69')
Lets assume we want to determine who all may depend on this IP Address.
We'll see if we can discover useful information from the used_by
field of this IP Address, or its Subnet...
>>> ipaddr.used_by
None
>>> subnet = ipaddr.get_parent()
>>> subnet.used_by
'Network & Telecom Services'
>>> ip_container = subnet.get_parent()
>>> ip_container.used_by
'University of Oregon (3582)'
This demonstrates programatic read-access to the Address Space in NetDot.
ℹ Similar to
get_parent
, you'll notice you canget_children
if you would like to!
Example: Update 'aliases' of Sites in NetDot
As a simple script example, imagine we want to update the 'aliases' with the string "(odd layout)" for some set of sites in NetDot. In this example, we will write a script to do just that.
For those who want to just see the script, there is the full code sample below
For starters, we need to set up a Repository
to interact with NetDot.
import netdot
repo = netdot.Repository(...) # Provide credentials, e.g. via environment variables using `os` module
Now, we are given a list of SITE_IDS
to which we want to update the 'alias' with the string "(odd layout)".
We can use Python list comprehensions along with the handy replace()
method!
Note that all
site
's fields may be referenced when using thesite.replace()
method!
SITE_IDS = [98, 124, 512, 123, 111]
sites = [ repo.get_site(id) for id in SITE_IDS ]
updated_sites = [ site.replace(aliases=f'{site.aliases}(odd layout)') for site in sites ]
Then, it is time to actually apply the updates to NetDot!
for updated_site in updated_sites:
response = repo.update_site(updated_site.id, updated_site)
print(response)
Code Sample for Example: Update 'aliases' of Sites in NetDot
The full code sample is provided here.
import netdot
repo = netdot.Repository(...) # Provide credentials, e.g. via environment variables using `os` module
SITE_IDS = [98, 124, 512, 123, 111]
sites = [ repo.get_site(id) for id in SITE_IDS ]
updated_sites = [ site.replace(aliases=f'{site.aliases}(odd layout)') for site in sites ]
for site in sites:
response = repo.update_site(site.id, updated_site)
Primary Interface: netdot.Repository
netdot.Repository
is the type of object that gets returned from thenetdot.connect()
function.
The netdot.Repository
class is the primary interface of this package.
It provides many methods to download data from NetDot (e.g. get_site, get_device, get_devices_by_site, get_all_sites...)
⚠ TODO: ... Work in progres!!
There are many NetDot entities yet to be added to this
netdot.Repository
interface!In the meantime, consider using the Legacy Interface for anything not found here.
Legacy Interface: netdot.Client.Connect()
The legacy netdot.Client.Connect()
class is still available at this time!
This can be used to access all aspects of the NetDot RESTful API.
See the legacy example code that was provided with this Connect class:
⚠ This example has been untested for a very long time!
#!/usr/bin/env python2.7
import sys
import os
import netdot
uname = 'my_user'
pword = 'my_pass'
server = "https://netdot.localdomain/netdot"
debug = 1
dot = netdot.Client.Connect(uname, pword, server, [debug])
# Direct GET/POST/DELETE calls
r = dot.get('/host?name=my-server-name')
r = dot.post('/host', host)
name = dot.get_host_by_name('foo')
cname = dot.add_cname_to_record('foo','bar.foo.example.com')
ipid = dot.get_host_by_ipid('11111')
rrid = dot.get_host_by_rrid('11111')
ipblock = dot.get_ipblock("184.171.96.0/24")
user = dot.get_person_by_username('mary')
user_id = dot.get_person_by_id('111')
host = {
'name':'my-server',
'subnet':'192.168.1.0/24',
'ethernet':'XX:XX:XX:XX:XX:XX',
'info':'My Server'
}
r = dot.create_host(host)
print r
Appendix: Using help()
in interpreter
When in the interpreter session, it can be very useful to run help(netdot.Repository)
to learn more about the Repository's capabilities.
ℹ If you've already established a connection via
repo = netdot.connect()
, then you can runhelp(repo)
instead!
>>> help(netdot.Repository)
class Repository(builtins.object)
| Work with Netdot API using Python objects.
|
| Methods defined here:
|
| __init__(self, netdot_url, user, password, parallelization_factor=11, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| get_asset(repo:netdot_sites_manager.netdot.repository.Repository, id:int) -> ~T
| Get info about a Asset from Netdot.
|
| Args: ... trimmed for brevity...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file netdot-0.2.3.tar.gz
.
File metadata
- Download URL: netdot-0.2.3.tar.gz
- Upload date:
- Size: 52.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 192e7249a40775c1c477e00bd3d87044e46411868be9e9d5f358a9f83d7c345e |
|
MD5 | 6c6bab79a15216de3b15c754ad612f67 |
|
BLAKE2b-256 | ff2a885609d3633b8b545917382dd0727fda38fbb1b052db368be48f69152e84 |
File details
Details for the file netdot-0.2.3-py2.py3-none-any.whl
.
File metadata
- Download URL: netdot-0.2.3-py2.py3-none-any.whl
- Upload date:
- Size: 33.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8ae2aa793ada55945ba9937e102ef2015c971be93675e8954e0fa1ec0ab93ae |
|
MD5 | bfef644370f53e21e207336b66a263c6 |
|
BLAKE2b-256 | 733780428dfa70210dc9fa32e52cfeb71944a1ad53245de3ab29aacedb1138ff |