2014-09-24 151 views
0

我有以下代码在snmp v2上为整个1.3.6.1 OIDRoot的特定IP获取批量。然后我将结果写入一个csv文件。问题在于它只写了最后的结果行而不是所有的行。我是C#的新手,仍然非常了解这门语言。你能告诉我在我的代码中我做错了什么吗?SNMPSHARPNET - GETBULK只写最后一个OID

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

这是CSV文件的内容。

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 

非常感谢您的帮助。

+0

我不是C#程序员,但似乎没有必要为每个返回的OID打开和关闭文件。您应该可以在'foreach'循环之外调用'新的FileStream'。 – Jolta 2014-09-24 08:10:39

回答

2

要么使用FileMode.Append,要么在第一个foreach循环之前打开文件。您正在有效覆盖每个响应的文件。