В настоящее время я говорю о CyberPower PDU, который по сути является мощным снабжение управляемыми розетками с помощью следующей команды get_data:
Код: Выделить всё
def get_data(ip_address: str, object_identity: str) -> int | str:
"""Get the OID's value. Only integer and string values are currently supported."""
iterator = getCmd(
SnmpEngine(),
CommunityData("public", mpModel=0),
UdpTransportTarget(transportAddr=(ip_address, 161), timeout=1, retries=0),
ContextData(),
ObjectType(ObjectIdentity(object_identity)),
)
error_indication, error_status, error_index, variable_bindings = next(iterator)
if error_indication:
raise RuntimeError(str(error_indication))
elif error_status:
raise RuntimeError(str(error_status))
else:
[variable_binding] = variable_bindings
[_oid, value] = variable_binding
return convert_snmp_type_to_python_type(value)
Поэтому я попытался изменить свою команду на что-нибудь. вот так:
Код: Выделить всё
def get_multiple_data(ip_address: str, object_identities: list[str]) -> int | str:
"""Get the OID's value. Only integer and string values are currently supported."""
# The OID for retrieving an outlet's state is hardcoded for debugging purposes
ids = [".1.3.6.1.4.1.3808.1.1.3.3.5.1.1.4.{}".format(outlet) for outlet in range(1, 17)]
oids = [ObjectType(ObjectIdentity(id)) for id in ids]
print("OIDs: " + str(oids))
iterator = getCmd(
SnmpEngine(),
CommunityData("public", mpModel=0),
UdpTransportTarget(transportAddr=(ip_address, 161), timeout=10, retries=0),
ContextData(),
*oids,
)
error_indication, error_status, error_index, variable_bindings = next(iterator)
...
Я знаю, что существует также BulkCmd, но я не совсем уверен, как его использовать, поскольку SNMP для меня новый. и довольно загадочно.
Резюме: у меня есть список OID:
Код: Выделить всё
ids = [".1.3.6.1.4.1.3808.1.1.3.3.5.1.1.4.{}".format(outlet) for outlet in range(1, 17)]
Подробнее здесь: https://stackoverflow.com/questions/780 ... es-in-snmp