class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.nodename = self.nd.keys()[0] self.device = self.nd.values()[0] self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 1) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device] self.assertEqual(expected, self.nd.all()) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual(expected, self.nd.search(self.nodename)) self.assertEqual(expected, self.nd.search('17', field='onCallID')) self.assertEqual(expected, self.nd.search('juniper', field='vendor')) def test_match(self): """Test the match() method.""" expected = [self.device] self.assertEqual(expected, self.nd.match(nodename=self.nodename)) self.assertEqual(expected, self.nd.match(vendor='juniper')) self.assertNotEqual(expected, self.nd.match(vendor='cisco')) def tearDown(self): NetDevices._Singleton = None
def get_bulk_acls(): """ Returns a dict of acls with an applied count over settings.AUTOLOAD_BULK_THRESH """ from trigger.netdevices import NetDevices nd = NetDevices() all_acls = defaultdict(int) for dev in nd.all(): for acl in dev.acls: all_acls[acl] += 1 bulk_acls = {} for acl, count in all_acls.items(): if count >= settings.AUTOLOAD_BULK_THRESH and acl != '': bulk_acls[acl] = count return bulk_acls
class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.device = self.nd[DEVICE_NAME] self.device2 = self.nd[DEVICE2_NAME] self.nodename = self.device.nodeName self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 2) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device, self.device2] self.assertEqual(sorted(expected), sorted(self.nd.all())) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual([self.device], self.nd.search(self.nodename)) self.assertEqual(self.nd.all(), self.nd.search('17', field='onCallID')) def test_match(self): """Test the match() method.""" self.assertEqual([self.device], self.nd.match(nodename=self.nodename)) self.assertEqual(self.nd.all(), self.nd.match(vendor='juniper')) self.assertEqual([], self.nd.match(vendor='cisco')) def test_multiple_filter_match(self): """Test that passing multiple kwargs filters properly.""" # There should be only one Juniper router. self.assertEqual(self.nd.match(nodename='test1-abc'), self.nd.match(vendor='juniper', devicetype='router')) # And only one Juniper switch. self.assertEqual(self.nd.match(nodename='test2-abc'), self.nd.match(vendor='juniper', devicetype='switch')) def test_match_with_null_value(self): """Test the match() method when attr value is ``None``.""" self.device.site = None # Zero it out! expected = [self.device] # None raw self.assertEqual(expected, self.nd.match(site=None)) # "None" string self.assertEqual(expected, self.nd.match(site='None')) # Case-insensitive attr *and* value self.assertEqual(expected, self.nd.match(SITE='NONE')) def test_reload(self): """Test the .reload() method.""" nd = self.nd nd.reload() self.assertEqual(nd, self.nd) def tearDown(self): _reset_netdevices()
from trigger import tacacsrc #from trigger.conf import settings from trigger.netdevices import NetDevices from trigger.contrib.commando.plugins import gather_info #settings.NETDEVICES_SOURCE = os.path.abspath('netdevices.csv') #settings.DEFAULT_REALM = 'MyRealm' #os.environ['TRIGGER_ENABLEPW'] = \ # tacacsrc.get_device_password(settings.DEFAULT_REALM).password #device_list = ['dc3-core-nxos1', 'dc3-core-nxos2', 'dc3-edge', 'dc3-gw1', # 'dc3-gw2', 'dc3-nxos1', 'dc3-nxos2', 'dc3-sw1'] nd = NetDevices() devices = nd.all() #for d in nd.all(): # if d.vendor('Cisco'): # devices = append(d) gi = gather_info.GatherInfo(devices) gi.run() print(gi.parsed_results) #for host, value in gi.results: # print(host, value) """ Plugin for gathering info from devices. Only Cisco currently (including ASA). Will try to parse as much info from commands and place as key-values under
class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.device = self.nd[DEVICE_NAME] self.device2 = self.nd[DEVICE2_NAME] self.nodename = self.device.nodeName self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 2) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device, self.device2] self.assertEqual(sorted(expected), sorted(self.nd.all())) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual([self.device], self.nd.search(self.nodename)) self.assertEqual(self.nd.all(), self.nd.search('17', field='onCallID')) def test_match(self): """Test the match() method.""" self.assertEqual([self.device], self.nd.match(nodename=self.nodename)) self.assertEqual(self.nd.all(), self.nd.match(vendor='juniper')) self.assertEqual([], self.nd.match(vendor='cisco')) def test_multiple_filter_match(self): """Test that passing multiple kwargs filters properly.""" # There should be only one Juniper router. self.assertEqual( self.nd.match(nodename='test1-abc'), self.nd.match(vendor='juniper', devicetype='router') ) # And only one Juniper switch. self.assertEqual( self.nd.match(nodename='test2-abc'), self.nd.match(vendor='juniper', devicetype='switch') ) def test_match_with_null_value(self): """Test the match() method when attr value is ``None``.""" self.device.site = None # Zero it out! expected = [self.device] # None raw self.assertEqual(expected, self.nd.match(site=None)) # "None" string self.assertEqual(expected, self.nd.match(site='None')) # Case-insensitive attr *and* value self.assertEqual(expected, self.nd.match(SITE='NONE')) def tearDown(self): _reset_netdevices()
# Reading the report file and writing to the master file. with open(str(report_file_date), 'r') as report_file, open('cfgfiles/master_report_' + datetime.date.today().isoformat() + '.txt', 'a') as master_report: if len(report_file.readlines()) < 1: # Adding device as first line in report. master_report.write('\n\n*** Device: ' + each_device + ' ***\n') master_report.write('\n' + 'No Configuration Changes Recorded On ' + datetime.datetime.now().isoformat() + '\n\n\n') else: # Appending the content to the master report file. report_file.seek(0) master_report.write('\n\n*** Device: ' + each_device + ' ***\n\n') master_report.write(report_file.read()) # Defining the list of devices to monitor. These are my LAB01 devices. nd = NetDevices() neteng_devices = nd.all() #devices = sorted(neteng_devices) #print(devices) # Extracting the running config to a file, depending on the device vendor (Cisco, Juniper or Arista). for each_device in neteng_devices: # Check the device to see if it is reachable. if not each_device.is_reachable(): print 'Sorry.. %s, was unreachable.' % format(each_device) trash = each_device.dump() else: # Using Trigger to check for the each_device vendor. if str(each_device.vendor) == 'cisco': diff_function('cisco_ios', 'cisco', 'rlaney', 'ralrox', 'show running') # Using Trigger to check for the each_device vendor. elif str(each_device.vendor) == 'juniper':
#!/Users/rlaney/.virtualenvs/NetEngineerONE/bin/python from __future__ import absolute_import, division, print_function import json from trigger.cmds import Commando from trigger.netdevices import NetDevices nd = NetDevices() device_list = nd.all() class ShowMeTheMoney(Commando): """ Execute the following on a list of Cisco devices: """ vendors = ['cisco'] commands = [ 'terminal length 0', 'show ip int brief', 'show interface status', 'show ip arp', ] if __name__ == '__main__': showstuff = ShowMeTheMoney(devices=nd) showstuff.run() # Commando exposes this to start the event loop print('\nUnparsed Results:\n')