Unexpected results with value assignment in a foreach loop - sharpsnmplib

207 views Asked by At

I am looking for the explanation of the following problem: I use the sharpsnmplib to communicate with devices on the network. If I assign the result to an "outside" variable, then the result of the Messenger.Walk method contains zero elements, if I assign the results inside a method, and return it, then the method has proper results.

I feel like I miss something very obvious, but I cannot find it. What makes this more interesting, is that the following bad code "works" on 200 networks, but has problems with some devices on a specific network.

Update: I forgot this: sharpsnmplib's Messenger.Walk takes a List as an argument, and adds it's results to it. https://github.com/lextm/sharpsnmplib/blob/038a3a0272f688075f573490721194e40cd56e3f/SharpSnmpLib/Messaging/Messenger.cs#L689

The "unreliable code":

        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);
            }
        }

The code that works:

        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;
        }
0

There are 0 answers