17
17
using MessageReceivedEventArgs = Samples . Pipeline . MessageReceivedEventArgs ;
18
18
using System . Threading ;
19
19
using System . Threading . Tasks ;
20
+ using Mono . Options ;
21
+
20
22
// USE_SOURCE_GENERATOR conditional symbol is automatically defined in the project file
21
23
// when MibSourceGenerator is detected
22
24
#if USE_SOURCE_GENERATOR
@@ -29,11 +31,34 @@ internal static class Program
29
31
{
30
32
public static async Task Main ( string [ ] args )
31
33
{
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
33
46
{
47
+ options . Parse ( args ) ;
48
+ }
49
+ catch ( OptionException ex )
50
+ {
51
+ Console . WriteLine ( $ "Error: { ex . Message } ") ;
52
+ ShowHelp ( options ) ;
34
53
return ;
35
54
}
36
-
55
+
56
+ if ( showHelp )
57
+ {
58
+ ShowHelp ( options ) ;
59
+ return ;
60
+ }
61
+
37
62
var idEngine161 = ByteTool . Convert ( "80004fb805636c6f75644dab22cc" ) ;
38
63
var store = new ObjectStore ( ) ;
39
64
@@ -153,11 +178,12 @@ public static async Task Main(string[] args)
153
178
154
179
var pipelineFactory = new SnmpApplicationFactory ( store , membership , handlerFactory ) ;
155
180
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 ) ) ;
157
182
engine . Listener . ExceptionRaised += Engine_ExceptionRaised ;
158
183
engine . Listener . MessageReceived += RequestReceived ;
159
184
engine . Start ( ) ;
160
185
Console . WriteLine ( "#SNMP is available at https://sharpsnmp.com" ) ;
186
+ Console . WriteLine ( $ "SNMP agent listening on port { port } ") ;
161
187
162
188
Console . WriteLine ( "Press Ctrl+C to stop . . . " ) ;
163
189
var cancellationTokenSource = new CancellationTokenSource ( ) ;
@@ -176,5 +202,14 @@ private static void RequestReceived(object sender, MessageReceivedEventArgs e)
176
202
{
177
203
Console . WriteLine ( "Message version {0}: {1}" , e . Message . Version , e . Message ) ;
178
204
}
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
+ }
179
214
}
180
215
}
0 commit comments