我机器上架了一个snmp agent simulator
第一次接触 请问c#如何与模拟的设备交互(获取设备信息)
这边我网上搜了个库SnmpSharpNet
这是代码:
// SNMP community name
            OctetString community = new OctetString("public");            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);
            // Set SNMP version to 1 (or 2)
            param.Version = SnmpVersion.Ver1;
            // Construct the agent address object
            // IpAddress class is easy to use here because
            //  it will try to resolve constructor parameter if it doesn't
            //  parse to an IP address
            IpAddress agent = new IpAddress("127.0.0.1");            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);执行到这里异常 
            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            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);            // If result is null then agent didn't reply or we couldn't parse the reply.
            if (result != null)
            {
                // ErrorStatus other then 0 is an error returned by 
                // the Agent - see SnmpConstants for error definitions
                if (result.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                        result.Pdu.ErrorStatus,
                        result.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList
                    Console.WriteLine("sysDescr({0}) ({1}): {2}",
                        result.Pdu.VbList[0].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
                        result.Pdu.VbList[0].Value.ToString());
                    Console.WriteLine("sysObjectID({0}) ({1}): {2}",
                        result.Pdu.VbList[1].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
                        result.Pdu.VbList[1].Value.ToString());
                    Console.WriteLine("sysUpTime({0}) ({1}): {2}",
                        result.Pdu.VbList[2].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
                        result.Pdu.VbList[2].Value.ToString());
                    Console.WriteLine("sysContact({0}) ({1}): {2}",
                        result.Pdu.VbList[3].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
                        result.Pdu.VbList[3].Value.ToString());
                    Console.WriteLine("sysName({0}) ({1}): {2}",
                        result.Pdu.VbList[4].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
                        result.Pdu.VbList[4].Value.ToString());
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }
            target.Close();希望接触过的高手多多指点 非常感谢

解决方案 »

  1.   

    这台电脑上没SnmpSharpNet
    晚上回家有时间就帮你看下
    不过以前我没用过target,都是用的SimpleSnmp“第一次接触 请问c#如何与模拟的设备交互(获取设备信息)”
    在SnmpSharpNet里面没尝试过,以前都是直接和设备通信。这个就没法回答了
      

  2.   

    你这是访问自己电脑,又没访问设备。除非你装了Agent。
    莫非你的模拟程序是另一个进程?否则怎么会占用161端口?
    UdpTarget 这个类我没用过,不过SNMP访问的是SNMP Agent的161端口,自身又不会绑定161端口的
      

  3.   

    下面是基于SnmpSharpNet写的两个SnmpGet的方法
    家里电脑出问题,系统还原了,VS用不了了...  这是记事本打开拷下来的
    #region Get Request 
          /// <summary>
          /// 取单个节点
          /// </summary>
          /// <param name="ip">IP地址</param>
          /// <param name="community">共同体</param>
          /// <param name="oid">OID字符串</param>
          /// <param name="outstring">节点值的字符串表示(可能为null值)</param>
          /// <returns>0: 成功, -1: 失败, 1: IP错误, 2: OID为空</returns>
          protected static int Get(string ip, string community, string oid, out string outstring)
          {
             outstring = null;         int iret = 0;
             string[] oidlist = null;         if (!Utils.IsIpAddress(ip)) iret = 1;
             if (String.IsNullOrEmpty(community)) community = readcommunity;
             if (String.IsNullOrEmpty(oid)) iret = 2;         if (iret.Equals(0))
             {
                oidlist = new string[] { oid };
                SimpleSnmp snmp = new SimpleSnmp(ip, community);            if (snmp.Valid)
                {
                   Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver2, oidlist);               if (null != result)
                   {
                      foreach (KeyValuePair<Oid, AsnType> kvp in result)
                      { outstring = kvp.Value.ToString(); }
                   }
                   else
                       iret = -1;
                } 
                else 
                    iret = -1;
             }         return iret;
          }
          /// <summary>
          /// 取多个节点
          /// </summary>
          /// <param name="ip">IP地址</param>
          /// <param name="community">共同体</param>
          /// <param name="oidlist">OID字符串数组</param>
          /// <param name="outstrlist">多个节点值的字符串表示的数组(可能为null值)</param>
          /// <returns>0: 成功, -1: 失败, 1: IP错误, 2: OID为空</returns>
          protected static int Get(string ip, string community, string[] oidlist, out string[] outstrlist)
          {
             outstrlist = null;         int iret = 0;
             List<string> cols = new List<string>();         if (!Utils.IsIpAddress(ip)) iret = 1;
             if (String.IsNullOrEmpty(community)) community = readcommunity;
             if (null == oidlist) iret = 2;
             else
             {
                for (int i = 0; i < oidlist.Length; i++)
                {
                   if (String.IsNullOrEmpty(oidlist[i]))
                   {
                      iret = 2;
                      break;
                   }
                }
             }         if (iret.Equals(0))
             {
                SimpleSnmp snmp = new SimpleSnmp(ip, community);            if (snmp.Valid)
                {
                   Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver2, oidlist);               if (null != result)
                   {
                       // 避免可能因OID无效或为最后一个节点导致的错误
                       if (result.Count.Equals(oidlist.Length))
                       {
                           foreach (KeyValuePair<Oid, AsnType> kvp in result)
                           { cols.Add(kvp.Value.ToString()); }
                       }
                   }
                   else
                       iret = -1;
                } 
                else 
                    iret = -1;
             }         if (!cols.Count.Equals(0)) outstrlist = new string[cols.Count];
             if (null != outstrlist) cols.CopyTo(outstrlist);         return iret;
          }
          #endregion