def test_snmp_get(self): client = snmp_client.SNMPClient(self.host, self.port) OID = ((1, 3, 6, 1, 2, 1, 1, 1, 0), None) client.get_command(OID, callback=self.mock_callback) self.assertEqual("Siemens, SIMATIC, S7-200", self.result) log_item = self.log_queue.get(True, 2) self.assertIsInstance(log_item['timestamp'], datetime) self.assertEqual('127.0.0.1', log_item['remote'][0]) self.assertEquals('snmp', log_item['data_type'])
def test_snmp_set(self): client = snmp_client.SNMPClient(self.host, self.port) OID = ((1, 3, 6, 1, 2, 1, 1, 6, 0), rfc1902.OctetString('test comment')) client.set_command(OID, callback=self.mock_callback) # FIXME: no log entry for set commands client.get_command(OID, callback=self.mock_callback) self.assertEqual("test comment", self.result) get_log_item = self.log_queue.get(True, 5) self.assertIsInstance(get_log_item['timestamp'], datetime) self.assertEqual('127.0.0.1', get_log_item['remote'][0]) self.assertEquals('snmp', get_log_item['data_type'])
def test_snmp_set(self): """ Objective: Test if we can set data via snmp_set """ client = snmp_client.SNMPClient(self.host, self.port) OID = ((1, 3, 6, 1, 2, 1, 1, 6, 0), rfc1902.OctetString('test comment')) client.set_command(OID, callback=self.mock_callback) client.get_command(OID, callback=self.mock_callback) set_log_item = self.log_queue.get(True, 5) self.assertEqual("test comment", self.result) self.assertIsInstance(set_log_item['timestamp'], datetime) self.assertEqual('127.0.0.1', set_log_item['remote'][0]) self.assertEquals('snmp', set_log_item['data_type']) self.assertIn('SNMPv3 Set:', set_log_item['data'][0]['request']) get_log_item = self.log_queue.get(True, 5) self.assertEqual("test comment", self.result) self.assertIsInstance(get_log_item['timestamp'], datetime) self.assertEqual('127.0.0.1', get_log_item['remote'][0]) self.assertEquals('snmp', get_log_item['data_type']) self.assertIn('SNMPv3 Get:', get_log_item['data'][0]['request'])
def handle_startendtag(self, tag, attrs): """ handles template tags provided in XHTML notation. Expected format: <condata source="(engine)" key="(descriptor)" /> Example: <condata source="snmp" key="1.3.6.1.2.1.1.1" /> at the moment, the parser is space- and case-sensitive(!), this could be improved by using REGEX for replacing the template tags with actual values. """ source = '' key = '' # only parse tags that are conpot template tags ( <condata /> ) if tag == 'condata': # initialize original tag (needed for value replacement) origin = '<' + tag for attribute in attrs: # extend original tag origin = origin + ' ' + attribute[0] + '="' + attribute[1] + '"' # fill variables with all meta information needed to # gather actual data from the other engines (snmp, modbus, ..) if attribute[0] == 'source': source = attribute[1] elif attribute[0] == 'key': key = attribute[1] # finalize original tag origin += ' />' # we really need a key in order to do our work.. if key: # deal with snmp powered tags: if source == 'snmp': # initialize snmp client client = snmp_client.SNMPClient(self.snmp_host, self.snmp_port) # convert key to (int-)tuple filled OID descriptor key = key.split('.') key = (tuple(map(int, key)), None) client.get_command(key, callback=self.mock_snmp_callback) self.payload = self.payload.replace(origin, self.result) # deal with eval powered tags: elif source == 'eval': result = '' # evaluate key try: result = eval(key) except: pass self.payload = self.payload.replace(origin, result)