All, I am fairly new to python, but my approach to writing scripts is to try to use as few lines of code as possible without sacrificing readability. With that being said, that is what I tried to do with the script at hand.
I have a script that gets either the running or the startup configuration from a cisco switch via Pysnmp and sends it to a TFTP server. This requires that the value of multiple OID's be set on the switch. When I tried to set the value multiple OID's on a single setCmd
command via snmp as so:
g = setCmd(SnmpEngine(),
CommunityData(snmp_community),
UdpTransportTarget((host_ip, 161)),
ContextData(),
ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopyProtocol', random_number), 1),
#ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopySourceFileType', random_number), users_numeric_entry),
#ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopyDestFileType', random_number), 1),
#ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopyServerAddress', random_number), '10.10.11.100'),
#ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopyFileName', random_number), destination_file),
#ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopyEntryRowStatus', random_number), 1)
)
next(g)
I can see on the switch(via debugs) that the SNMP packets are delivered, but there must be a timing issue when applying them(setting the OIDs' values). With that being said, is there a way to insert a few millisecond delay between each setCmd command?
When Set the value of each OID individually, my script works as expected, and the configuration is copied to the TFTP server. So for each OID that I need to set the value for, I have a setCmd
like below:
g = setCmd(SnmpEngine(),
CommunityData(snmp_community),
UdpTransportTarget((host_ip, 161)),
ContextData(),
ObjectType(ObjectIdentity('CISCO-CONFIG-COPY-MIB', 'ccCopySourceFileType', random_number),
users_numeric_entry))
next(g)
Is my problem with setting all OIDs in a single setCmd command really a delay issue? If so, can I insert a delay within the command?
Thank you for your time!
UPDATING WITH DEBUGS FROM SWITCH
Debugs when sending a single setCmd
:
00:19:16: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:19:16: SNMP: Get request, reqid 2179409, errstat 0, erridx 0
system.5.0 = NULL TYPE/VALUE
00:19:16: SNMP: Response, reqid 2179409, errstat 0, erridx 0
system.5.0 = Switch
00:19:16: SNMP: Packet sent via UDP to 10.10.10.10
00:19:17: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:19:17: SNMP: Set request, reqid 2179410, errstat 0, erridx 0
ccCopyEntry.2.21 = 1
ccCopyEntry.3.21 = 4
ccCopyEntry.4.21
Switch# = 1
ccCopyEntry.5.21 = 10.10.10.10
ccCopyEntry.6.21 = Switch-running.txt
ccCopyEntry.14.21 = 1
00:19:17: SNMP: Response, reqid 2179410, errstat 12, erridx 6
ccCopyEntry.2.21 = 1
ccCopyEntry.3.21 = 4
ccCopyEntry.4.21 = 1
ccCopyEntry.5.21 = 10.10.10.10
ccCopyEntry.6.21 = Switch-running.txt
ccCopyEntry.14.21 = 1
00:19:17: SNMP: Packet sent via UDP to 10.10.10.10
When I send separate setCmd
commands:
00:15:33: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:33: SNMP: Get request, reqid -1563913845, errstat 0, erridx 0
system.5.0 = NULL TYPE/VALUE
00:15:33: SNMP: Response, reqid -1563913845, errstat 0, erridx 0
system.5.0 = Switch
00:15:33: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid -1563913844, errstat 0, erridx 0
ccCopyEntry.2.102 = 1
00:15:34: SNMP: Response, req
Switch#id -1563913844, errstat 0, erridx 0
ccCopyEntry.2.102 = 1
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid -1563913843, errstat 0, erridx 0
ccCopyEntry.3.102 = 3
00:15:34: SNMP: Response, reqid -1563913843, errstat 0, erridx 0
ccCopyEntry.3.102 = 3
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid
Switch# -1563913842, errstat 0, erridx 0
ccCopyEntry.4.102 = 1
00:15:34: SNMP: Response, reqid -1563913842, errstat 0, erridx 0
ccCopyEntry.4.102 = 1
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid -1563913841, errstat 0, erridx 0
ccCopyEntry.5.102 = 10.0.1.81
00:15:34: SNMP: Response, reqid -1563913841, errstat 0, erridx 0
ccCopyEntry.5.102 = 10.0.1.81
00:15:34: SNMP: Packet sent via UDP to 10.0.
Switch#1.20
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid -1563913840, errstat 0, erridx 0
ccCopyEntry.6.102 = Switch--start.txt
00:15:34: SNMP: Response, reqid -1563913840, errstat 0, erridx 0
ccCopyEntry.6.102 = Switch--start.txt
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Set request, reqid -1563913839, errstat 0, erridx 0
ccCopyEntry.14.102 =
Switch#1
00:15:34: SNMP: Response, reqid -1563913839, errstat 0, erridx 0
ccCopyEntry.14.102 = 1
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
00:15:34: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:34: SNMP: Get request, reqid -1563913838, errstat 0, erridx 0
ccCopyEntry.10.102 = NULL TYPE/VALUE
00:15:34: SNMP: Response, reqid -1563913838, errstat 0, erridx 0
ccCopyEntry.10.102 = 2
00:15:34: SNMP: Packet sent via UDP to 10.10.10.10
Switch#
00:15:37: SNMP: Packet received via UDP from 10.10.10.10 on Vlan1
00:15:37: SNMP: Set request, reqid -1563913837, errstat 0, erridx 0
ccCopyEntry.14.102 = 6
00:15:37: SNMP: Response, reqid -1563913837, errstat 0, erridx 0
ccCopyEntry.14.102 = 6
00:15:37: SNMP: Packet sent via UDP to 10.10.10.10