Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarheidari committed Jan 31, 2023
1 parent 63431f7 commit 333dfee
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 2 deletions.
60 changes: 60 additions & 0 deletions samples/SetRequest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Arad.SnmpSharp;
using Arad.SnmpSharp.Types;
using System;
using System.Net;

namespace SetRequest
{
internal class Program
{
static void Main(string[] args)
{
// Prepare target
UdpTarget target = new UdpTarget((IPAddress)new IpAddress("host-name"));
// Create a SET PDU
Pdu pdu = new Pdu(PduType.Set);
// Set sysLocation.0 to a new string
pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.6.0"), new Octetstring("Some other value"));
// Set a value to integer
pdu.VbList.Add(new Oid("1.3.6.1.2.1.67.1.1.1.1.5.0"), new Integer32(500));
// Set a value to unsigned integer
pdu.VbList.Add(new Oid("1.3.6.1.2.1.67.1.1.1.1.6.0"), new UInteger32(101));
// Set Agent security parameters
AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new Octetstring("private"));
// Response packet
SnmpV2Packet response;
try
{
// Send request and wait for response
response = target.Request(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()));
}
}
}
}
}
12 changes: 12 additions & 0 deletions samples/SetRequest/SetRequest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Arad.SnmpSharp\Arad.SnmpSharp.csproj" />
</ItemGroup>

</Project>
90 changes: 90 additions & 0 deletions samples/Traprecv/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Net.Sockets;
using System.Net;
using Arad.SnmpSharp;

namespace Traprecv
{
internal class Program
{
static void Main(string[] args)
{
// Construct a socket and bind it to the trap manager port 162
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
EndPoint ep = (EndPoint)ipep;
socket.Bind(ep);
// Disable timeout processing. Just block until packet is received
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
bool run = true;
int inlen = -1;
while (run)
{
byte[] indata = new byte[16 * 1024];
// 16KB receive buffer int inlen = 0;
IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
EndPoint inep = (EndPoint)peer;
try
{
inlen = socket.ReceiveFrom(indata, ref inep);
}
catch (Exception ex)
{
Console.WriteLine("Exception {0}", ex.Message);
inlen = -1;
}
if (inlen > 0)
{
// Check protocol version int
int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
if (ver == (int)SnmpVersion.Ver1)
{
// Parse SNMP Version 1 TRAP packet
SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
pkt.decode(indata, inlen);
Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic);
Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific);
Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
Console.WriteLine("*** VarBind content:");
foreach (Vb v in pkt.Pdu.VbList)
{
Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
}
Console.WriteLine("** End of SNMP Version 1 TRAP data.");
}
else
{
// Parse SNMP Version 2 TRAP packet
SnmpV2Packet pkt = new SnmpV2Packet();
pkt.decode(indata, inlen);
Console.WriteLine("** SNMP Version 2 TRAP received from {0}:", inep.ToString());
if ((PduType)pkt.Pdu.Type != PduType.V2Trap)
{
Console.WriteLine("*** NOT an SNMPv2 trap ****");
}
else
{
Console.WriteLine("*** Community: {0}", pkt.Community.ToString());
Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
Console.WriteLine("*** VarBind content:");
foreach (Vb v in pkt.Pdu.VbList)
{
Console.WriteLine("**** {0} {1}: {2}",
v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
}
Console.WriteLine("** End of SNMP Version 2 TRAP data.");
}
}
}
else
{
if (inlen == 0)
Console.WriteLine("Zero length packet received.");
}
}
}
}
}
12 changes: 12 additions & 0 deletions samples/Traprecv/Traprecv.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Arad.SnmpSharp\Arad.SnmpSharp.csproj" />
</ItemGroup>

</Project>
16 changes: 15 additions & 1 deletion src/Arad.SnmpSharp/Arad.SnmpSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Arad.SnmpSharp", "Arad.SnmpSharp\Arad.SnmpSharp.csproj", "{51F79E20-0960-4F19-9B8F-870DC216B14B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{7486F79F-5907-4BC0-8AEA-C52ACEB647B8}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{7486F79F-5907-4BC0-8AEA-C52ACEB647B8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DCBBC64B-28E5-4013-87CD-3BFE0B10DC65}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Traprecv", "..\..\samples\Traprecv\Traprecv.csproj", "{2F52D022-4D51-4CB2-86FD-6543D74AA0DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SetRequest", "..\..\samples\SetRequest\SetRequest.csproj", "{9C191B2C-4607-4449-9293-493F938E81AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -19,12 +23,22 @@ Global
{51F79E20-0960-4F19-9B8F-870DC216B14B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51F79E20-0960-4F19-9B8F-870DC216B14B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51F79E20-0960-4F19-9B8F-870DC216B14B}.Release|Any CPU.Build.0 = Release|Any CPU
{2F52D022-4D51-4CB2-86FD-6543D74AA0DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F52D022-4D51-4CB2-86FD-6543D74AA0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F52D022-4D51-4CB2-86FD-6543D74AA0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F52D022-4D51-4CB2-86FD-6543D74AA0DE}.Release|Any CPU.Build.0 = Release|Any CPU
{9C191B2C-4607-4449-9293-493F938E81AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C191B2C-4607-4449-9293-493F938E81AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C191B2C-4607-4449-9293-493F938E81AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C191B2C-4607-4449-9293-493F938E81AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{51F79E20-0960-4F19-9B8F-870DC216B14B} = {DCBBC64B-28E5-4013-87CD-3BFE0B10DC65}
{2F52D022-4D51-4CB2-86FD-6543D74AA0DE} = {7486F79F-5907-4BC0-8AEA-C52ACEB647B8}
{9C191B2C-4607-4449-9293-493F938E81AF} = {7486F79F-5907-4BC0-8AEA-C52ACEB647B8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DBA82394-13A1-4422-A56C-6F596BA05186}
Expand Down
2 changes: 1 addition & 1 deletion src/Arad.SnmpSharp/Arad.SnmpSharp/Arad.SnmpSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyTitle>Arad.SnmpSharp</AssemblyTitle>
<Authors>Ammar Heidari</Authors>
<PackageTags>c-sharp dotnet dotnetcore snmp dotnet-core snmp-agent snmpv2-trap dotnet-standard snmpv1 snmpv3 dotnet-framework snmpv2 snmp-library</PackageTags>
<PackageProjectUrl>https://arad-itc.com/</PackageProjectUrl>
<PackageProjectUrl>https://github.com/araditc/Arad.SnmpSharp</PackageProjectUrl>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<SignAssembly>True</SignAssembly>
Expand Down

0 comments on commit 333dfee

Please sign in to comment.