2

我正在寻找以下问题的解释:我使用sharpsnmplib 与网络上的设备进行通信。如果我将结果分配给“外部”变量,则 Messenger.Walk 方法的结果包含零个元素,如果我在方法内部分配结果并返回它,则该方法具有正确的结果。

我觉得我错过了一些非常明显的东西,但我找不到它。更有趣的是,以下错误代码在 200 个网络上“有效”,但在特定网络上的某些设备上存在问题。

更新:我忘记了这一点:sharpsnmplib 的 Messenger.Walk 将 List 作为参数,并将其结果添加到其中。 https://github.com/lextm/sharpsnmplib/blob/038a3a0272f688075f573490721194e40cd56e3f/SharpSnmpLib/Messaging/Messenger.cs#L689

“不可靠的代码”:

        foreach (var item in devices.ToList())
        {
            try
            {
                List<Variable> res = new List<Variable>();

                BadMethod(item, res);
                logger.Debug(item.ipAddress + " count: " + res.Count);
            }

            catch (Exception er)
            {
                logger.Info("SNMP request error: " + er.Message);
                continue;
            }
        }

        private static int BadMethod(device devicesItem, List<Variable> res)
        {
            if (devicesItem.deviceType.snmpVersion == 1)
            {
                Messenger.Walk(VersionCode.V1,
                            new IPEndPoint(IPAddress.Parse(devicesItem.ipAddress), 161),
                            new OctetString(Properties.Test.Default.community),
                            new ObjectIdentifier("1.3.6.1.2.1.2.2.1.6"),
                            res,
                            14000,
                            WalkMode.WithinSubtree);
            }
            else if (devicesItem.deviceType.snmpVersion == 2)
            {
                Messenger.BulkWalk(VersionCode.V2,
                                new IPEndPoint(IPAddress.Parse(devicesItem.ipAddress), 161),
                                new OctetString(Properties.Test.Default.community),
                                new ObjectIdentifier("1.3.6.1.2.1.2.2.1.6"),
                                res,
                                14000,
                                1,
                                WalkMode.WithinSubtree,
                                null,
                                null);
            }
        }

有效的代码:

        foreach (var item in devices.ToList())
        {
            try
            {
                var res = GoodMethod(item);
                logger.Debug(item.ipAddress + " count: " + res.Count);
            }

            catch (Exception er)
            {
                logger.Info("SNMP request error: " + er.Message);
                continue;
            }
        }

        private static List<Variable> GoodMethod(device devicesItem)
        {
            List<Variable> res = new List<Variable>();
            if (devicesItem.deviceType.snmpVersion == 1)
            {
                Messenger.Walk(VersionCode.V1,
                            new IPEndPoint(IPAddress.Parse(devicesItem.ipAddress), 161),
                            new OctetString(Properties.Test.Default.community),
                            new ObjectIdentifier("1.3.6.1.2.1.2.2.1.6"),
                            res,
                            14000,
                            WalkMode.WithinSubtree);
            }
            else if (devicesItem.deviceType.snmpVersion == 2)
            {
                Messenger.BulkWalk(VersionCode.V2,
                                new IPEndPoint(IPAddress.Parse(devicesItem.ipAddress), 161),
                                new OctetString(Properties.Test.Default.community),
                                new ObjectIdentifier("1.3.6.1.2.1.2.2.1.6"),
                                res,
                                14000,
                                1,
                                WalkMode.WithinSubtree,
                                null,
                                null);
            }

            return res;
        }
4

0 回答 0