Example #1
0
def test_session_invalid_port(version):
    with pytest.raises(EasySNMPTimeoutError):
        session = Session(remote_port=1234,
                          version=version,
                          timeout=0.2,
                          retries=1)
        session.get('sysContact.0')
Example #2
0
def snmp_set_multiple(oid_values, **session_kargs):
    """
    Perform multiple SNMP SET operations to update various pieces of
    information at the same time.

    :param oid_values: a list of tuples whereby each tuple contains a
                       (oid, value) or an (oid, value, snmp_type)
    :param session_kargs: keyword arguments which will be sent used when
                          constructing the session for this operation;
                          all parameters in the Session class are supported
    """

    session = Session(**session_kargs)
    return session.set_multiple(oid_values)
Example #3
0
def test_session_update():
    s = Session(version=3)
    ptr = s.sess_ptr
    s.version = 1
    s.update_session()
    assert ptr != s.sess_ptr
    s.tunneled = True
    ptr = s.sess_ptr
    with pytest.raises(ValueError):
        s.update_session()
    assert ptr == s.sess_ptr
    s.update_session(tunneled=False, version=2)
    assert s.version == 2
    assert s.tunneled is False
Example #4
0
def snmp_get_next(oids, **session_kargs):
    """
    Uses an SNMP GETNEXT operation to retrieve the next variable after
    the chosen item.

    :param oids: you may pass in a list of OIDs or single item; each item
                 may be a string representing the entire OID
                 (e.g. 'sysDescr.0') or may be a tuple containing the
                 name as its first item and index as its second
                 (e.g. ('sysDescr', 0))
    :param session_kargs: keyword arguments which will be sent used when
                          constructing the session for this operation;
                          all parameters in the Session class are supported
    """

    session = Session(**session_kargs)
    return session.get_next(oids)
Example #5
0
def snmp_walk(oids='.1.3.6.1.2.1', **session_kargs):
    """
    Uses SNMP GETNEXT operation to automatically retrieve multiple
    pieces of information in an OID for you.

    :param oids: you may pass in a single item (multiple values currently
                 experimental) which may be a string representing the
                 entire OID (e.g. 'sysDescr.0') or may be a tuple
                 containing the name as its first item and index as its
                 second (e.g. ('sysDescr', 0))
    :param session_kargs: keyword arguments which will be sent used when
                          constructing the session for this operation;
                          all parameters in the Session class are supported
    """

    session = Session(**session_kargs)
    return session.walk(oids)
Example #6
0
def snmp_set(oid, value, type=None, **session_kargs):
    """
    Perform an SNMP SET operation to update a particular piece of
    information.

    :param oid: the OID that you wish to set which may be a string
                representing the entire OID (e.g. 'sysDescr.0') or may
                be a tuple containing the name as its first item and
                index as its second (e.g. ('sysDescr', 0))
    :param value: the value to set the OID to
    :param snmp_type: if a numeric OID is used and the object is not in
                      the parsed MIB, a type must be explicitly supplied
    :param session_kargs: keyword arguments which will be sent used when
                          constructing the session for this operation;
                          all parameters in the Session class are supported
    """

    session = Session(**session_kargs)
    return session.set(oid, value, type)
Example #7
0
def snmp_get_bulk(oids, non_repeaters=0, max_repetitions=10, **session_kargs):
    """
    Performs a bulk SNMP GET operation to retrieve multiple pieces of
    information in a single packet.

    :param oids: you may pass in a list of OIDs or single item; each item
                 may be a string representing the entire OID
                 (e.g. 'sysDescr.0') or may be a tuple containing the
                 name as its first item and index as its second
                 (e.g. ('sysDescr', 0))
    :param non_repeaters: the number of objects that are only expected to
                          return a single GETNEXT instance, not multiple
                          instances
    :param max_repetitions: the number of objects that should be returned
                            for all the repeating OIDs
    :param session_kargs: keyword arguments which will be sent used when
                          constructing the session for this operation;
                          all parameters in the Session class are supported
    """

    session = Session(**session_kargs)
    return session.get_bulk(oids, non_repeaters, max_repetitions)
Example #8
0
def snmp_bulkwalk(
    oids='.1.3.6.1.2.1', non_repeaters=0, max_repetitions=10,
    **session_kargs
):
    """
    Uses SNMP GETBULK operation using the prepared session to
    automatically retrieve multiple pieces of information in an OID

    :param oids: you may pass in a single item
                 * string representing the
                 entire OID (e.g. 'sysDescr.0')
                 * tuple (name, index) (e.g. ('sysDescr', 0))
                 * list of OIDs
    :param non_repeaters: the number of objects that are only expected to
                          return a single GETNEXT instance, not multiple
                          instances
    :param max_repetitions: the number of objects that should be returned
                            for all the repeating OIDs
    :return: a list of SNMPVariable objects containing the values that
             were retrieved via SNMP
    """

    session = Session(**session_kargs)
    return session.bulkwalk(oids, non_repeaters, max_repetitions)
Example #9
0
def test_session_hostname_and_remote_port_split(version):
    session = Session(hostname='localhost:162', version=version)
    assert session.hostname == 'localhost'
    assert session.remote_port == 162
Example #10
0
def test_session_invalid_hostname_and_remote_port(version):
    with pytest.raises(ValueError):
        Session(hostname='localhost:162', remote_port=163, version=version)
Example #11
0
def test_session_invalid_hostname(version):
    with pytest.raises(EasySNMPConnectionError):
        session = Session(hostname='invalid', version=version)
        session.get('sysContact.0')
Example #12
0
def test_session_invalid_snmp_version():
    with pytest.raises(ValueError):
        Session(version=4)
Example #13
0
def test_session_invalid_port(version):
    with pytest.raises(EasySNMPTimeoutError):
        session = Session(
            remote_port=1234, version=version, timeout=0.2, retries=1
        )
        session.get('sysContact.0')
Example #14
0
def test_session_invalid_hostname(version):
    with pytest.raises(EasySNMPConnectionError):
        session = Session(hostname='invalid', version=version)
        session.get('sysContact.0')