etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
576 stars 194 forks source link

'setCmd' is not defined #462

Open A-Belean opened 1 month ago

A-Belean commented 1 month ago

from pysnmp.hlapi import * import time # Import the time module

SNMP parameters

snmp_community = "private" # Replace with your SNMP community string snmp_target = ("192.168.1.100", 161) # Replace with your device's IP address and SNMP port

Function to send an SNMP SET command with delays

def send_snmp_set_with_delay(community, ip, port, relay_number, value, delay_seconds): try: relay_oid = f"1.3.6.1.4.1.42505.6.2.3.1.3.{relaynumber}" , , , varBinds = next( setCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, port)), ContextData(), ObjectType(ObjectIdentity(relay_oid), Integer(value))) )

    for name, val in varBinds:
        print(f"OID: {name}, Value: {val}")

    # Add a delay (pause) after sending the command
    time.sleep(delay_seconds)

except Exception as e:
    print(f"Error: {e}" )

Loop to switch off relays 1 to 9 with a 2-second delay between each relay

for relay_number in range(0, 10): value = 0 # Switch off the relay (0 for off) delay_seconds = 2 # Specify the delay duration in seconds send_snmp_set_with_delay(snmp_community, "192.168.1.100", 161, relay_number, value, delay_seconds)

send_snmp_set_with_delay(snmp_community, "192.168.1.100", 161, 13, 0, 0)

print("Lifetime stopped again") input("Press Enter to exit...")

Any help with this specific error or where am I wrong Error: name 'setCmd' is not defined

lextm commented 1 month ago

Posting issues here is invalid. Please read #429 for more details.

Known issue due to breaking changes in release 6.2, https://docs.lextudio.com/pysnmp/upgrade#upgrade-to-6-2-releases