snmp error ( Reached max try ) Need help to catch the trap in...

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
snmp max tried error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SnmpSharpNet;

namespace snmpwalk
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
          
                OctetString community = new OctetString("public");
                AgentParameters param = new AgentParameters(community);
                param.Version = SnmpVersion.Ver2;
                IpAddress agent = new IpAddress("10.120.7.202");
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
                Oid rootOid = new Oid("1.3.6.1.4.1.27338.5"); // ifDescr
                Oid lastOid = (Oid)rootOid.Clone();
                Pdu pdu = new Pdu(PduType.GetBulk);
                pdu.NonRepeaters = 0;
                pdu.MaxRepetitions = 5;
                while (lastOid != null)
                {
                    if (pdu.RequestId != 0)
                    {
                        pdu.RequestId += 1;
                    }
                    pdu.VbList.Clear();
                    pdu.VbList.Add(lastOid);
                  SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);[/COLOR] // This give a error as maximum tried attempts, and like to catch this, so the programm is not stopping......
                    if (result != null)
                    {
                        if (result.Pdu.ErrorStatus != 0)
                        {
                        textBox2.Text= "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))
                                {
                                textBox1.Text = textBox1.Text +"{0} ({1}): {2}"+
                                        SnmpConstants.GetTypeName(v.Value.Type)+
                                        v.Value.ToString();
                                    if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                        lastOid = null;
                                    else
                                        lastOid = v.Oid;
                                }
                                else
                                {
                                    lastOid = null;
                                }
                            }
                        }
                    }
                    else
                    {
                    textBox2.Text = "No response received from SNMP agent.";
                    }
                }
                target.Close();
            }
        }
    }
 
Last edited:
Does line 43 throw an exception, or does it return a null?
 
Exception with code 13 of SnmpSharpNet

PS : im novice
1646503090040.png
 
So what is keeping you from putting a try{ } around that line, and then a catch (SnmpException ex) { } in that lines following the try block?
 
Back
Top Bottom