SNMPSHARPNET - GETBULK only writing last OID

1.2k Views Asked by At

I have the following code which does a get bulk on snmp v2 for a specific IP for the whole 1.3.6.1 OIDRoot. I am then writing the result to a csv file. The issue is that it is only writing the last result line rather than all the lines. I am new to C# and still very much learning the language. Could you please tell me what i am doing wrong in my code?

private void SNMP_WALK(object sender, EventArgs e)
    {
        OctetString community = new OctetString("public");
        AgentParameters param = new AgentParameters(community);

        param.Version = SnmpVersion.Ver2;

        string deviceMac = null;
        string devicePort = null;
        string deviceHCID = null;


                XmlDocument doc = new XmlDocument();
                doc.Load("devices.xml");

                foreach (XmlElement dev in doc.SelectNodes("/data/devices/device"))
                {
                    deviceMac = dev.Attributes["mac"].Value;
                    devicePort = dev.Attributes["port"].Value;
                    deviceHCID = dev.Attributes["hcid"].Value;

                    FileStream ostrm;
                    StreamWriter writer;
                    TextWriter oldOut = Console.Out;

                    if (deviceHCID != null)
                    {                           
                                try
                                    {
                                        IpAddress agent = new IpAddress(devicePort);
                                        UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
                                        Oid rootOid = new Oid("1.3.6.1");

                                        Oid lastOid = (Oid)rootOid.Clone();
                                        Pdu pdu = new Pdu(PduType.GetBulk);

                                        pdu.NonRepeaters = 0;
                                        pdu.MaxRepetitions = 20;

                                        while (lastOid != null)
                                        {
                                        if (pdu.RequestId != 0)
                                            {
                                                pdu.RequestId += 1;
                                            }
                                            pdu.VbList.Clear();
                                            pdu.VbList.Add(lastOid);

                                            SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
                                            if (result != null)
                                                {
                                                if (result.Pdu.ErrorStatus != 0)
                                                {
                                                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                                    result.Pdu.ErrorStatus,
                                                    result.Pdu.ErrorIndex);
                                                    lastOid = null;
                                                    break;
                                                }
                                                else
                                                {
                                                    foreach (Vb v in result.Pdu.VbList)
                                                    {
                                                    if (rootOid.IsRootOf(v.Oid))
                                                        {
                                                            string deviceHCIDw = deviceHCID;

                                                            ostrm = new FileStream("snmp_dump.csv", FileMode.OpenOrCreate, FileAccess.Write);
                                                            writer = new StreamWriter(ostrm);

                                                            Console.SetOut(writer);
                                                            Console.WriteLine("{0} {1} ({2}): {3}",
                                                            deviceHCIDw.ToString(),
                                                            v.Oid.ToString(),
                                                            SnmpConstants.GetTypeName(v.Value.Type),
                                                            v.Value.ToString());                                                                                                                      
                                                            Console.SetOut(oldOut);
                                                            writer.Close();
                                                            ostrm.Close();
                                                            Console.WriteLine("Done");

                                                            if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                                            lastOid = null;
                                                            else
                                                            lastOid = v.Oid;
                                                        }
                                                    else
                                                    {
                                                        lastOid = null;
                                                    }
                                                }
                                                }
                                            }
                                            else
                                            {
                                                Console.WriteLine("No response received from SNMP agent.");
                                            }
                                       }
                                        target.Close();
                               }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                       }
                  }
            }

This is the CSV file contents.

HCID01318 1.3.6.1.6.3.18.1.1.1.8.116.48.48.48 (Unknown): SNMP End-of-MIB-View
00 A1 C0 A8 0A FC
91 5E
d-only
PRINTER;DES:CANON 640NPCL_PS;
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00       00 00 00 00 00 00 00 00 00 00 00
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Thank you in advance for your help.

1

There are 1 best solutions below

0
On

Either use FileMode.Append, or open the file before the first foreach loop. You are effectively overwriting the file for each response.