Beispiel #1
0
        /// <summary>
        /// Perform an SNMP Set.
        /// </summary>
        /// <param name="destination">The destination host.</param>
        /// <param name="oid">The OID to set.</param>
        /// <param name="value">The value to set.</param>
        /// <returns>Nothing.</returns>
        public static async Task SnmpSetAsync(IPAddress destination, string community, Oid oid, AsnType value)
        {
            UdpTarget target = new UdpTarget(destination);

            // Create a SET PDU
            Pdu pdu = new Pdu(EPduType.Set);

            pdu.VbList.Add(oid, value);

            // Set Agent security parameters
            AgentParameters aparam = new AgentParameters(ESnmpVersion.Ver2, new OctetString(community));

            // Response packet
            SnmpV2Packet response;

            try
            {
                // Send request and wait for response
                response = await target.RequestAsync(pdu, aparam) as SnmpV2Packet;
            }
            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Console.WriteLine(string.Format("Request failed with exception: {0}", ex.Message));
                target.Close();
                return;
            }

            // Make sure we received a response
            if (response == null)
            {
                Console.WriteLine("Error in sending SNMP request.");
            }
            else
            {
                // Check if we received an SNMP error from the agent
                if (response.Pdu.ErrorStatus != 0)
                {
                    Console.WriteLine(string.Format("SNMP agent returned ErrorStatus {0} on index {1}", response.Pdu.ErrorStatus, response.Pdu.ErrorIndex));
                }
                else
                {
                    // Everything is ok. Agent will return the new value for the OID we changed
                    Console.WriteLine(string.Format("Agent response {0}: {1}", response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
                }
            }
        }
Beispiel #2
0
        void timer_Tick(object sender, EventArgs e)
        {
            AgentParameters param = new AgentParameters(SnmpVersion.Ver2, new OctetString(App.snmpCommunity));

            param.DisableReplySourceCheck = !App.snmpCheckSrcFlag;
            target = new UdpTarget((IPAddress)ip, App.snmpPort, App.snmpTimeout, App.snmpRetry);
            Pdu pdu = new Pdu(PduType.Get);

            for (int i = 0; i < requestOids.Length; i++)
            {
                pdu.VbList.Add(requestOids[i]);
            }
            try
            {
                target.RequestAsync(pdu, param, new SnmpAsyncResponse(SnmpAsyncResponseCallback));
            }
            catch (System.Exception ex)
            {
                TipMessage = "发送数据包异常";
            }
        }