Skip to content

Commit 657408d

Browse files
committed
Add command line options for SNMP agent configuration
1 parent 7b1e953 commit 657408d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Samples/CSharpCore/snmpd/Program.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using MessageReceivedEventArgs = Samples.Pipeline.MessageReceivedEventArgs;
1818
using System.Threading;
1919
using System.Threading.Tasks;
20+
using Mono.Options;
21+
2022
// USE_SOURCE_GENERATOR conditional symbol is automatically defined in the project file
2123
// when MibSourceGenerator is detected
2224
#if USE_SOURCE_GENERATOR
@@ -29,11 +31,34 @@ internal static class Program
2931
{
3032
public static async Task Main(string[] args)
3133
{
32-
if (args.Length != 0)
34+
// Default port value
35+
int port = 161;
36+
bool showHelp = false;
37+
38+
// Parse command line options
39+
var options = new OptionSet
40+
{
41+
{ "p|port=", "SNMP agent port number (default: 161)", (int p) => port = p },
42+
{ "h|help", "Show this help message and exit", h => showHelp = h != null }
43+
};
44+
45+
try
3346
{
47+
options.Parse(args);
48+
}
49+
catch (OptionException ex)
50+
{
51+
Console.WriteLine($"Error: {ex.Message}");
52+
ShowHelp(options);
3453
return;
3554
}
36-
55+
56+
if (showHelp)
57+
{
58+
ShowHelp(options);
59+
return;
60+
}
61+
3762
var idEngine161 = ByteTool.Convert("80004fb805636c6f75644dab22cc");
3863
var store = new ObjectStore();
3964

@@ -153,11 +178,12 @@ public static async Task Main(string[] args)
153178

154179
var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory);
155180
using var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup(idEngine161));
156-
engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, 1610));
181+
engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, port));
157182
engine.Listener.ExceptionRaised += Engine_ExceptionRaised;
158183
engine.Listener.MessageReceived += RequestReceived;
159184
engine.Start();
160185
Console.WriteLine("#SNMP is available at https://sharpsnmp.com");
186+
Console.WriteLine($"SNMP agent listening on port {port}");
161187

162188
Console.WriteLine("Press Ctrl+C to stop . . . ");
163189
var cancellationTokenSource = new CancellationTokenSource();
@@ -176,5 +202,14 @@ private static void RequestReceived(object sender, MessageReceivedEventArgs e)
176202
{
177203
Console.WriteLine("Message version {0}: {1}", e.Message.Version, e.Message);
178204
}
205+
206+
private static void ShowHelp(OptionSet options)
207+
{
208+
Console.WriteLine("Usage: snmpd [OPTIONS]");
209+
Console.WriteLine("SNMP agent sample using #SNMP Library.");
210+
Console.WriteLine();
211+
Console.WriteLine("Options:");
212+
options.WriteOptionDescriptions(Console.Out);
213+
}
179214
}
180215
}

Samples/CSharpCore/snmpd/snmpd.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PropertyGroup>
1414
<ItemGroup>
1515
<PackageReference Include="Lextm.SharpSnmpLib" Version="12.5.6" />
16+
<PackageReference Include="Mono.Options.Core" Version="1.0.0" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

0 commit comments

Comments
 (0)