总而言之,我对python很新,但我编写脚本的方法是尝试尽可能少地使用代码行而不牺牲可读性。话虽如此,这就是我尝试用手头的脚本做的事情。
我有一个脚本,通过Pysnmp从cisco交换机获取运行或启动配置,并将其发送到TFTP服务器。这要求在交换机上设置多个OID的值。当我尝试通过snmp在单个setCmd
command上设置多个OID值时:
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)
我可以在交换机(通过调试)上看到SNMP数据包已经传送,但在应用它们时必须存在时间问题(设置OID的值)。 With that being said, is there a way to insert a few millisecond delay between each setCmd command?
当单独设置每个OID的值时,我的脚本按预期工作,并将配置复制到TFTP服务器。因此,对于我需要设置值的每个OID,我有一个像下面这样的setCmd
:
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)
我在单个setCmd命令中设置所有OID的问题确实是一个延迟问题吗?如果是这样,我可以在命令中插入延迟吗?
感谢您的时间!
从交换机更新调试
发送单个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
当我发送单独的setCmd
命令时:
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
该条目遵循qstxswpoi中详述的RowStatus语义
在我看来,您需要将ccCopyEntryRowStatus设置为createAndGo而不是active。
我不认为您可以在cisco开关内部使用SNMP代理来单独处理具有延迟的SNMP变量。你(作为一个用户)期望照顾时间也是非常可疑的...
所以我可能会试着理解为什么交换机不喜欢你的管理命令,管理它的正确方法是什么......一个可能的灵感来源是MIB中描述你正在处理的变量的描述。