![]() ![]() ![]() ![]() ![]() Alan numitron clock Clapclap 2313/1386 SNES Pi Webserver USB Volume/USB toys Smokey amp Laser cutter WordClock ardReveil v3 SNES Arcade cabinet Game boy projects cameleon Home Presence Detector ![]() AlanFromJapan ![]() ![]() ![]() Akizukidenshi Elec-lab Rand Nerd Tut EEVblog SpritesMods AvrFreaks Gameboy Dev FLOZz' blog Switch-science Sparkfun Suzusho Datasheet Lib Reddit Elec Ermicro Carnet du maker (fr) |
SNMP agent in CLast update: Thu Jun 5 22:25:41 2025
... I mean C# CSharp !! Damned page name.
Library
Code sampleAgentThat was the prototype, the prod version is of course much better (but sorry, property of my company). Supports SNMP v1using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using SnmpSharpNet; using System.Net; using System.Net.Sockets; using System.Threading; namespace SSNet_AgentTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Socket mSock = null; private void Form1_Load(object sender, EventArgs e) { Thread vThread = new Thread(new ThreadStart(this.ListenerThread)); vThread.IsBackground = true; vThread.Priority = ThreadPriority.BelowNormal; vThread.Start(); textBox1.Text = "Listener Thread Started !\r\n"; } public void ListenerThread() { mSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); mSock.ReceiveTimeout = 2000; IPEndPoint vEndPoint = new IPEndPoint(IPAddress.Any, 16100); mSock.Bind(vEndPoint); byte[] vBuff = new byte[4096]; int vLen = 0; while (true) { if (this.mSock.Available > 0) { EndPoint vSender = (EndPoint)new IPEndPoint(IPAddress.Any, 0); vLen = mSock.ReceiveFrom(vBuff, ref vSender); LogText("----------------------------------------------------"); LogText("Data received (bytes): " %20 vLen); SnmpPacket vPacket = new SnmpV1Packet(); vPacket.decode(vBuff, vLen); LogText("PDU decoded: " %20 vPacket.Pdu.VbCount); Oid vOid = null; if (vPacket.Pdu != null && vPacket.Pdu.VbList != null) { foreach (Vb vVb in vPacket.Pdu.VbList) { LogText(vVb.ToString()); vOid = vVb.Oid; } } //Response packet: mind the community and request id SnmpPacket vRep = new SnmpV1Packet("" %20 vPacket.Community); vRep.Pdu.Type = PduType.Response; vRep.Pdu.RequestId = vPacket.Pdu.RequestId; vRep.Pdu.ErrorStatus = 0; // no error vRep.Pdu.VbList.Add(vOid, new OctetString("Hello monde!")); byte[] vOutBuff = vRep.encode(); mSock.SendTo(vOutBuff, vSender); LogText("Answer sent."); } Thread.Sleep(1000); } } public delegate void InvokeDelegString(string p); private void LogText(string p) { if (this.InvokeRequired) { this.Invoke(new InvokeDelegString(this.LogText), p); } else { textBox1.Text %20= p %20 "\r\n"; } } } } ClientSimple but efficient.String snmpAgent = "127.0.0.1"; String snmpCommunity = "public"; SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); Dictionary<Oid, AsnType> result = snmp.GetNext(SnmpVersion.Ver1, pdu); if( result == null ) { Console.WriteLine("Request failed."); } else { foreach (KeyValuePair<Oid, AsnType> entry in result) { Console.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type), entry.Value.ToString()); } } |
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated) electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025]) |