using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SnmpSharpNet;
using System.Net;
namespace Snmp_zy
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
           
            OctetString community = new OctetString(this.textBox3.Text.Trim());
          
            AgentParameters param = new AgentParameters(community
            param.Version = SnmpVersion.Ver2            IpAddress agent = new IpAddress(this.textBox1.Text.Trim());          
            UdpTarget target = new UdpTarget((IPAddress)agent, int.Parse(this.textBox2.Text.Trim()), 900, 0);
            Pdu pdu = new Pdu(PduType.Get);//Pdu-协议数据单元
         //   VbList VarBind list  [OID变量绑定列表]
            //pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
            //pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
            //pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
            //pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
            //pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName,注意,这5个一起绑定,如果有一个出错,5个都将不能成功
            pdu.VbList.Add(this.textBox5.Text.Trim());//this.textBox5.Text=1.3.6.1.2.1.31.1.1.1.10.4227866624
     //就报出错,整数溢出,我发现当oid在int32(有符号整数范围内就不报错),但是实际给出后面的索引号是无符号的uint32,程序在此无法编译通过,急盼解答      
      SnmpV2Packet result = null;
            try
            {
                 result = (SnmpV2Packet)target.Request(pdu, param);
            }
            catch (SnmpException e1)            {
                MessageBox.Show("注意了:出错了,可能是IP地址不对,网络不通,或者团体名不对.");
            }            if (result != null)
            {
   
                if (result.Pdu.ErrorStatus != 0)
                {
    
                    this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n",
                        result.Pdu.ErrorStatus,
                        result.Pdu.ErrorIndex);
                }
                else
                {
   
                    this.txtContent.Text += string.Format("sysDescr({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
                        result.Pdu.VbList[0].Value.ToString());
                }
            }
            else
            {
                this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
            }
            target.Dispose();
        }