def testSnmpV3SecLevels(self): """Check accepted security levels""" auth = "MD5" priv = "DES" snmp.Session(host="localhost", version=3, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=priv, privpassword="******") snmp.Session(host="localhost", version=3, seclevel=snmp.SNMP_SEC_LEVEL_NOAUTH, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=priv, privpassword="******") snmp.Session(host="localhost", version=3, seclevel=snmp.SNMP_SEC_LEVEL_AUTHNOPRIV, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=priv, privpassword="******")
def testSnmpV3Protocols(self): """Check accepted auth and privacy protocols""" for auth in ["MD5", "SHA"]: for priv in ["AES", "AES128", "DES"]: snmp.Session(host="localhost", version=3, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=priv, privpassword="******") self.assertRaises(ValueError, snmp.Session, host="localhost", version=3, secname="readonly", authprotocol="NOEXIST", authpassword="******", privprotocol="AES", privpassword="******") self.assertRaises(ValueError, snmp.Session, host="localhost", version=3, secname="readonly", authprotocol="MD5", authpassword="******", privprotocol="NOEXIST", privpassword="******")
def testInexistantNone(self): """Get an inexistant value but request none""" params = self.setUpSession(self.agent, 'public') params['none'] = True session = snmp.Session(**params) oid, a = session.get((1, 2, 3))[0] self.assertEqual(a, None)
def testSnmpV3(self): """Check initialization of SNMPv3 session""" snmp.Session(host="localhost", version=3, secname="readonly", authprotocol="MD5", authpassword="******", privprotocol="AES", privpassword="******")
def setUp(self): self.session = snmp.Session( host="127.0.0.1:{0}".format(self.agent.port), version=3, secname="read-write", authprotocol="MD5", authpassword="******", privprotocol="AES", privpassword="******")
def testSnmpV3SecLevels(self): """Check accepted security levels""" auth = "MD5" priv = "DES" snmp.Session(host="localhost", version=3, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=priv, privpassword="******") snmp.Session(host="localhost", version=3, secname="readonly", authprotocol=None, privprotocol=None) snmp.Session(host="localhost", version=3, secname="readonly", authprotocol=auth, authpassword="******", privprotocol=None)
def _test(self, ipv6, host): m = agent.TestAgent(ipv6) session = snmp.Session(host="{0}:{1}".format(host, m.port), community="public", version=2) try: ooid = mib.get('SNMPv2-MIB', 'sysDescr').oid + (0, ) oid, a = session.get(ooid)[0] self.assertEqual(a, b"Snimpy Test Agent public") finally: m.terminate()
def run(i): params = self.setUpSession(agents[i], 'community{0}'.format(i)) session = snmp.Session(**params) session.timeout = 10 * 1000 * 1000 oid, a = session.get(ooid)[0] exp = ("Snimpy Test Agent community{0}".format(i)).encode('ascii') with lock: if oid == ooid and \ a == exp: successes.append("community{0}".format(i)) else: failures.append("community{0}".format(i))
def testSeveralSessions(self): """Test with two sessions""" agent2 = self.addAgent('private', 'private-authpass', 'private-privpass') params = self.setUpSession(agent2, 'private') session2 = snmp.Session(**params) ooid = mib.get('SNMPv2-MIB', 'sysDescr').oid + (0, ) oid1, a1 = self.session.get(ooid)[0] oid2, a2 = session2.get(ooid)[0] self.assertEqual(oid1, ooid) self.assertEqual(oid2, ooid) self.assertEqual(a1, b"Snimpy Test Agent public") self.assertEqual(a2, b"Snimpy Test Agent private")
def testRepresentation(self): """Test session representation""" s = snmp.Session(host="localhost", community="public", version=1) self.assertEqual(repr(s), "Session(host=localhost,version=1)")
def testSnmpV2(self): """Check initialization of SNMPv2 session""" snmp.Session(host="localhost", community="public", version=2)
def setUp(self): self.session = snmp.Session( host="127.0.0.1:{0}".format(self.agent.port), community="public", version=self.version)
def setUp(self): self.session = snmp.Session(host="localhost", community="public", version=2)
def __init__(self, host="localhost", community="public", version=2, cache=False, none=False, timeout=None, retries=None, loose=False, bulk=40, # SNMPv3 secname=None, authprotocol=None, authpassword=None, privprotocol=None, privpassword=None): """Create a new SNMP manager. Some of the parameters are explained in :meth:`snmp.Session.__init__`. :param host: The hostname or IP address of the agent to connect to. Optionally, the port can be specified separated with a double colon. :type host: str :param community: The community to transmit to the agent for authorization purpose. This parameter is ignored if the specified version is 3. :type community: str :param version: The SNMP version to use to talk with the agent. Possible values are `1`, `2` (community-based) or `3`. :type version: int :param cache: Should caching be enabled? This can be either a boolean or an integer to specify the cache timeout in seconds. If `True`, the default timeout is 5 seconds. :type cache: bool or int :param none: Should `None` be returned when the agent does not know the requested OID? If `True`, `None` will be returned when requesting an inexisting scalar or column. :type none: bool :param timeout: Use the specified value in seconds as timeout. :type timeout: int :param retries: How many times the request should be retried? :type retries: int :param loose: Enable loose typing. When type coercion fails (for example when a MIB declare an element to be an ASCII string while it is not), just return the raw result instead of an exception. This mode should be enabled with caution. Patching the MIB is a better idea. :type loose: bool :param bulk: Max-repetition to use to speed up MIB walking with `GETBULK`. Set to `0` to disable. :type bulk: int """ if host is None: host = Manager._host self._host = host self._session = snmp.Session(host, community, version, secname, authprotocol, authpassword, privprotocol, privpassword, bulk=bulk) if timeout is not None: self._session.timeout = int(timeout * 1000000) if retries is not None: self._session.retries = retries if cache: if cache is True: self._session = CachedSession(self._session) else: self._session = CachedSession(self._session, cache) if none: self._session = NoneSession(self._session) self._loose = loose self._loaded = loaded # To be able to clone, we save the arguments provided to the # constructor in a generic way frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) self._constructor_args = dict((a, values[a]) for a in args if a != 'self')
def setUp(self): mib.load('IF-MIB') mib.load('SNMPv2-MIB') self.session = snmp.Session(host="localhost", community="public", version=self.version)
def setUp(self): params = self.setUpSession(self.agent, 'public') self.session = snmp.Session(**params)
def __init__( self, host="localhost", community="public", version=2, cache=False, none=False, timeout=None, retries=None, # SNMPv3 secname=None, authprotocol=None, authpassword=None, privprotocol=None, privpassword=None): """Create a new SNMP manager. Some of the parameters are explained in :meth:`snmp.Session.__init__`. :param host: The hostname or IP address of the agent to connect to. Optionally, the port can be specified separated with a double colon. :type host: str :param community: The community to transmit to the agent for authorization purpose. This parameter is ignored if the specified version is 3. :type community: str :param version: The SNMP version to use to talk with the agent. Possible values are `1`, `2` (community-based) or `3`. :type version: int :param cache: Should caching be enabled? This can be either a boolean or an integer to specify the cache timeout in seconds. If `True`, the default timeout is 5 seconds. :type cache: bool or int :param none: Should `None` be returned when the agent does not know the requested OID? If `True`, `None` will be returned when requesting an inexisting scalar or column. :type none: bool :param timeout: Use the specified value in seconds as timeout. :type timeout: int :param retries: How many times the request should be retried? :type retries: int """ if host is None: host = Manager._host self._host = host self._session = snmp.Session(host, community, version, secname, authprotocol, authpassword, privprotocol, privpassword) if timeout is not None: self._session.timeout = int(timeout * 1000000) if retries is not None: self._session.retries = retries if cache: if cache is True: self._session = CachedSession(self._session) else: self._session = CachedSession(self._session, cache) if none: self._session = NoneSession(self._session)