所有,我对python还是很陌生,但是我编写脚本的方法是在不牺牲可读性的情况下尝试使用尽可能少的代码行。话虽如此,这就是我尝试使用手头脚本的方式。
我有一个脚本,该脚本可通过Pysnmp从cisco交换机获取运行或启动配置,并将其发送到TFTP服务器。这要求在交换机上设置多个OID的值。当我尝试通过snmp在单个setCmd
命令上设置多个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
答案 0 :(得分:1)
我认为您不能使cisco交换机内的SNMP代理程序延迟地单独处理SNMP变量。您(作为用户)应该注意时间安排也很可疑...
因此,我可能会尝试理解为什么该开关不喜欢您的管理命令,什么是正确的管理方式。...一种可能的灵感来源是MIB中您要处理的变量的描述。定义。
答案 1 :(得分:1)
该条目遵循https://tools.ietf.org/html/rfc2579
中详细介绍的RowStatus语义在我看来,您需要将ccCopyEntryRowStatus设置为createAndGo而不是active。