-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from re import compile | ||
from json import loads | ||
|
||
from requests import session, post | ||
|
||
|
||
re_ipv4 = compile(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)') | ||
|
||
|
||
class NetXMS(object): | ||
def __init__(self, address, username, password, tls_verify, unknown): | ||
self.netxms = Api(address, username, password, tls_verify) | ||
self.unknown = unknown | ||
self.hosts = list() | ||
|
||
def run(self): | ||
objects = self.netxms.all() | ||
|
||
for obj in objects['objects']: | ||
address = description = '' | ||
try: | ||
if obj['ipAddressList']: | ||
for ip in obj['ipAddressList']: | ||
if re_ipv4.match(ip) and not ip.startswith('127.'): | ||
address = ip | ||
break | ||
else: | ||
continue | ||
except KeyError: | ||
continue | ||
try: | ||
description = obj['objectName'] | ||
except KeyError: | ||
description = self.unknown | ||
|
||
if address: | ||
self.hosts.append((address, description)) | ||
|
||
|
||
class Api(object): | ||
def __init__(self, base_url, username, password, tls_verify=False): | ||
self.base_url = base_url | ||
self.session = session() | ||
self.session.post( | ||
f'{self.base_url}/sessions', | ||
json={'login':username, 'password':password} | ||
) | ||
|
||
def all(self): | ||
return loads(self.session.get(f'{self.base_url}/objects').text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
#!/bin/sh | ||
# | ||
# Before running these tests, you must define some | ||
# environment variables, such as: | ||
# | ||
# $ export NETBOX_ADDRESS="https..." | ||
# $ export NETBOX_TOKEN="..." | ||
# $ export NMAP_PATH="..." | ||
# Before running these tests, fill the environment variables | ||
# below according to your setup. If you don't want to | ||
# hardcode this data, just be sure to exporting them in | ||
# your shell. | ||
## | ||
|
||
export NETBOX_ADDRESS="" | ||
export NETBOX_TOKEN="" | ||
|
||
export NMAP_PATH="" | ||
|
||
export PRIME_ADDRESS="" | ||
export PRIME_USER="" | ||
export PRIME_PASS="" | ||
|
||
export NETXMS_ADDRESS="" | ||
export NETXMS_USER="" | ||
export NETXMS_PASS="" | ||
|
||
|
||
python -m unittest tests.test_netbox | ||
python -m unittest tests.test_nmap | ||
python -m unittest tests.test_prime | ||
python -m unittest tests.test_netxms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
from os import environ | ||
from nbs.netxms import NetXMS | ||
|
||
|
||
class TestRequest(unittest.TestCase): | ||
def test_api(self): | ||
address = environ.get('NETXMS_ADDRESS') | ||
username = environ.get('NETXMS_USER') | ||
password = environ.get('NETXMS_PASS') | ||
|
||
netxms = NetXMS(address, username, password, False, 'unknown') | ||
self.assertIsInstance(netxms, NetXMS) | ||
netxms.run() | ||
self.assertIsInstance(netxms.hosts, list) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |