}//将对应id的条目的值设置为data private void GetPrinterResponse(object input_id) { string id = input_id.ToString(); string ipaddr = null; for (int i = 0; i < Common.dt_printer.Rows.Count; i++) { string tmp = Common.dt_printer.Rows[i]["Id"].ToString(); if (tmp == (string)id) { ipaddr = Common.dt_printer.Rows[i]["dev_ip"].ToString(); } } int feq = Common.getfequency((string)id); OctetString community = new OctetString("public"); AgentParameters param = new AgentParameters(community); param.Version = SnmpVersion.Ver1; IpAddress agent = new IpAddress(ipaddr); UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); Pdu pdu = new Pdu(PduType.Get); pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.1"); for (; ;) { if (canceltokensource.IsCancellationRequested == true) { Common.writetologfrm(string.Format("打印机 设备ID:{1} 线程{0}终止", Thread.CurrentThread.ManagedThreadId, id.ToString())); target.Dispose(); this.Close(); break;//若收到cancellationToken消息则取消此线程 } try { SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param); if (result != null) { if (result.Pdu.ErrorStatus != 0) { Common.writetologfrm(string.Format("打印机 ID:{0} 获取信息异常码{1}在{2}上", (string)id, result.Pdu.ErrorStatus, result.Pdu.ErrorIndex)); } else { int i = Convert.ToInt32(result.Pdu.VbList[0].Value.ToString()); this.Invoke(new MethodInvoker(() => { show_data(id, i); })); } } else { Common.writetologfrm(string.Format("打印机 ID:{0} 未收到回复", (string)id)); } } catch { Common.writetologfrm(string.Format("打印机 ID:{0} 获取信息异常", (string)id)); } Thread.Sleep(Common.getfequency((string)id)); } //target.Dispose(); }//Printer线程工作内容
//SNMP get方法 private string getSnmp(string oid, string host, int port) { string hostIP; int Port; string Oid; string get = ""; Oid = oid; hostIP = host; Port = port; //共同体类型 OctetString community = new OctetString("public"); AgentParameters param = new AgentParameters(community); //设置snmp版本 param.Version = SnmpVersion.Ver2; IpAddress agent = new IpAddress(hostIP); //构建目标 UdpTarget target = new UdpTarget((IPAddress)agent, port, 2000, 1); Pdu pdu = new Pdu(PduType.Get); pdu.VbList.Add(Oid); //发送snmp请求 SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param); 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 this.textBox1.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } else { // Reply variables are returned in the same order as they were added // to the VbList //result.Pdu.VbList[0].Oid.ToString() //SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) //result.Pdu.VbList[0].Value.ToString()) get = result.Pdu.VbList[0].Value.ToString(); } target.Dispose(); } return(get); }
}//将对应id的条目的状态栏设置为橘红色 private void GetSNMPResponse(object input_id) { //inputmodel temp =new inputmodel(); //temp = (inputmodel)mod; //CancellationTokenSource token = temp.canceltoken; string id = (string)input_id; string ipaddr = null; for (int i = 0; i < Common.dt_snmp.Rows.Count; i++) { string tmp = Common.dt_snmp.Rows[i]["Id"].ToString(); if (tmp == (string)id) { ipaddr = Common.dt_snmp.Rows[i]["dev_ip"].ToString(); } } int feq = Common.getfequency((string)id); //填充snmp包头信息 OctetString community = new OctetString("public"); //社区 AgentParameters param = new AgentParameters(community); //版本 param.Version = SnmpVersion.Ver1; //ip地址代理 IpAddress agent = new IpAddress(ipaddr); //定义udp包 UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); Pdu pdu = new Pdu(PduType.Get); //填充pdu列表 pdu.VbList.Add(".1.3.6.1.2.1.1.3.0"); //设备名的OID号 for (; ;) { if (canceltokensource.IsCancellationRequested == true) { Common.writetologfrm(string.Format("SNMP 设备ID:{1} 线程{0}终止", Thread.CurrentThread.ManagedThreadId, id.ToString())); target.Dispose(); this.Close(); break;//若收到cancellationToken消息则取消此线程 } Dictionary <string, string> output_cache = new Dictionary <string, string>(); try { //创建snmp请求 SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param); if (result != null) { //errorstatus不等于0则错误 if (result.Pdu.ErrorStatus != 0) { this.Invoke(new MethodInvoker(() => show_failed((string)id))); } else { //正确则返回pdu数据 for (int x = 0; x < result.Pdu.VbList.Count; x++) { this.Invoke(new MethodInvoker(() => show_success((string)id))); output_cache.Add(result.Pdu.VbList[x].Oid.ToString(), result.Pdu.VbList[x].Value.ToString()); } } } else { this.Invoke(new MethodInvoker(() => show_failed((string)id))); } } catch (Exception e) { this.Invoke(new MethodInvoker(() => Common.writetologfrm(string.Format("SNMP异常 ID:{0} 描述:{1} ", id.ToString(), e.Message)))); this.Invoke(new MethodInvoker(() => show_failed((string)id))); continue; } finally { Thread.Sleep(Common.getfequency((string)id)); } } //target.Dispose(); }//SNMP线程工作内容
private void button11_Click(object sender, EventArgs e) { //SnmpTest snt = new SnmpTest(tbAddress.Text, "public", ".1.3.6.1.4.1.38446"); //string[] t= {".1.3.6.1.4.1.38446"}; //snt.Test(); OctetString community = new OctetString("public"); AgentParameters param = new AgentParameters(community); param.Version = SnmpVersion.Ver2; IpAddress agent = new IpAddress("192.168.1.120"); UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); Pdu pdu2 = new Pdu(PduType.GetBulk); pdu2.VbList.Add(".1.3.6.1.4.1.38446.1.1.2.1.13"); SnmpV2Packet result2 = (SnmpV2Packet)target.Request(pdu2, param); int i = result2.Pdu.VbCount; string str = SnmpConstants.GetTypeName(result2.Pdu.VbList[0].Value.Type); string str2 = result2.Pdu.VbList[0].Value.ToString(); return; // Define Oid that is the root of the MIB // tree you wish to retrieve Oid rootOid = new Oid(".1.3.6.1.4.1.38446.1.5.9.1.9"); // ifDescr // This Oid represents last Oid returned by // the SNMP agent Oid lastOid = (Oid)rootOid.Clone(); // Pdu class used for all requests Pdu pdu = new Pdu(PduType.GetBulk); // In this example, set NonRepeaters value to 0 pdu.NonRepeaters = 0; // MaxRepetitions tells the agent how many Oid/Value pairs to return // in the response. pdu.MaxRepetitions = 5; // Loop through results while (lastOid != null) { // When Pdu class is first constructed, RequestId is set to 0 // and during encoding id will be set to the random value // for subsequent requests, id will be set to a value that // needs to be incremented to have unique request ids for each // packet if (pdu.RequestId != 0) { pdu.RequestId += 1; } // Clear Oids from the Pdu class. pdu.VbList.Clear(); // Initialize request PDU with the last retrieved Oid pdu.VbList.Add(lastOid); // Make SNMP request SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param); // You should catch exceptions in the Request if using in real application. // 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); lastOid = null; break; } else { // Walk through returned variable bindings foreach (Vb v in result.Pdu.VbList) { // Check that retrieved Oid is "child" of the root OID if (rootOid.IsRootOf(v.Oid)) { Console.WriteLine("{0} ({1}): {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString()); lastOid = v.Oid; } else { // we have reached the end of the requested // MIB tree. Set lastOid to null and exit loop lastOid = null; } } } } else { Console.WriteLine("No response received from SNMP agent."); } } target.Dispose(); }