資料來源 :
CodePlexProject Hosting for Open Source Software
lextm / sharpsnmplib : https://github.com/lextm/sharpsnmplib
C# Based Open Source SNMP for .NET and Mono
1: // typical usage
2: // snmpget -c=public -v=1 localhost 1.3.6.1.2.1.1.1.0
3: // snmpget -v=3 -l=authPriv -a=MD5 -A=authentication -x=DES -X=privacy -u=user localhost 1.3.6.1.2.1.1.1.0
4: using System;
5: using System.Collections.Generic;
6: using System.Linq;
7: using System.Net;
8: using System.Net.Sockets;
9: using Lextm.SharpSnmpLib;
10: using Lextm.SharpSnmpLib.Security;
11: using Lextm.SharpSnmpLib.Messaging;
12: using Mono.Options;
13:
14: namespace SnmpGet
15: {
16: internal static class Program
17: {
18: public static void Main(string[] args)
19: {
20: string community = "public";
21: bool showHelp = false;
22: bool showVersion = false;
23: VersionCode version = VersionCode.V1;
24: int timeout = 1000;
25: int retry = 0;
26: Levels level = Levels.Reportable;
27: string user = string.Empty;
28: string authentication = string.Empty;
29: string authPhrase = string.Empty;
30: string privacy = string.Empty;
31: string privPhrase = string.Empty;
32: bool dump = false;
33:
34: OptionSet p = new OptionSet()
35: .Add("c:", "Community name, (default is public)", delegate (string v) { if (v != null) community = v; })
36: .Add("l:", "Security level, (default is noAuthNoPriv)", delegate (string v)
37: {
38: if (v.ToUpperInvariant() == "NOAUTHNOPRIV")
39: {
40: level = Levels.Reportable;
41: }
42: else if (v.ToUpperInvariant() == "AUTHNOPRIV")
43: {
44: level = Levels.Authentication | Levels.Reportable;
45: }
46: else if (v.ToUpperInvariant() == "AUTHPRIV")
47: {
48: level = Levels.Authentication | Levels.Privacy | Levels.Reportable;
49: }
50: else
51: {
52: throw new ArgumentException("no such security mode: " + v);
53: }
54: })
55: .Add("a:", "Authentication method (MD5 or SHA)", delegate (string v) { authentication = v; })
56: .Add("A:", "Authentication passphrase", delegate(string v) { authPhrase = v; })
57: .Add("x:", "Privacy method", delegate (string v) { privacy = v; })
58: .Add("X:", "Privacy passphrase", delegate (string v) { privPhrase = v; })
59: .Add("u:", "Security name", delegate(string v) { user = v; })
60: .Add("h|?|help", "Print this help information.", delegate (string v) { showHelp = v != null; })
61: .Add("V", "Display version number of this application.", delegate (string v) { showVersion = v != null; })
62: .Add("d", "Display message dump", delegate(string v) { dump = true; })
63: .Add("t:", "Timeout value (unit is second).", delegate (string v) { timeout = int.Parse(v) * 1000; })
64: .Add("r:", "Retry count (default is 0)", delegate (string v) { retry = int.Parse(v); })
65: .Add("v|version:", "SNMP version (1, 2, and 3 are currently supported)", delegate (string v)
66: {
67: switch (int.Parse(v))
68: {
69: case 1:
70: version = VersionCode.V1;
71: break;
72: case 2:
73: version = VersionCode.V2;
74: break;
75: case 3:
76: version = VersionCode.V3;
77: break;
78: default:
79: throw new ArgumentException("no such version: " + v);
80: }
81: });
82:
83: if (args.Length == 0)
84: {
85: ShowHelp(p);
86: return;
87: }
88:
89: List<string> extra;
90: try
91: {
92: extra = p.Parse(args);
93: }
94: catch (OptionException ex)
95: {
96: Console.WriteLine(ex.Message);
97: return;
98: }
99:
100: if (showHelp)
101: {
102: ShowHelp(p);
103: return;
104: }
105:
106: if (extra.Count < 2)
107: {
108: Console.WriteLine("invalid variable number: " + extra.Count);
109: return;
110: }
111:
112: if (showVersion)
113: {
114: Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
115: return;
116: }
117:
118: IPAddress ip;
119: bool parsed = IPAddress.TryParse(extra[0], out ip);
120: if (!parsed)
121: {
122: foreach (IPAddress address in
123: Dns.GetHostAddresses(extra[0]).Where(address => address.AddressFamily == AddressFamily.InterNetwork))
124: {
125: ip = address;
126: break;
127: }
128:
129: if (ip == null)
130: {
131: Console.WriteLine("invalid host or wrong IP address found: " + extra[0]);
132: return;
133: }
134: }
135:
136: try
137: {
138: List<Variable> vList = new List<Variable>();
139: for (int i = 1; i < extra.Count; i++)
140: {
141: Variable test = new Variable(new ObjectIdentifier(extra[i]));
142: vList.Add(test);
143: }
144:
145: IPEndPoint receiver = new IPEndPoint(ip, 161);
146: if (version != VersionCode.V3)
147: {
148: foreach (
149: Variable variable in
150: Messenger.Get(version, receiver, new OctetString(community), vList, timeout))
151: {
152: Console.WriteLine(variable);
153: }
154:
155: return;
156: }
157:
158: if (string.IsNullOrEmpty(user))
159: {
160: Console.WriteLine("User name need to be specified for v3.");
161: return;
162: }
163:
164: IAuthenticationProvider auth = (level & Levels.Authentication) == Levels.Authentication
165: ? GetAuthenticationProviderByName(authentication, authPhrase)
166: : DefaultAuthenticationProvider.Instance;
167:
168: IPrivacyProvider priv;
169: if ((level & Levels.Privacy) == Levels.Privacy)
170: {
171: priv = new DESPrivacyProvider(new OctetString(privPhrase), auth);
172: }
173: else
174: {
175: priv = new DefaultPrivacyProvider(auth);
176: }
177:
178: Discovery discovery = Messenger.NextDiscovery;
179: ReportMessage report = discovery.GetResponse(timeout, receiver);
180:
181: GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(user), vList, priv, Messenger.MaxMessageSize, report);
182:
183: ISnmpMessage response = request.GetResponse(timeout, receiver);
184: if (dump)
185: {
186: Console.WriteLine(ByteTool.Convert(request.ToBytes()));
187: }
188:
189: if (response.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
190: {
191: throw ErrorException.Create(
192: "error in response",
193: receiver.Address,
194: response);
195: }
196:
197: foreach (Variable v in response.Pdu().Variables)
198: {
199: Console.WriteLine(v);
200: }
201: }
202: catch (SnmpException ex)
203: {
204: Console.WriteLine(ex);
205: }
206: catch (SocketException ex)
207: {
208: Console.WriteLine(ex);
209: }
210: }
211:
212: private static void ShowHelp(OptionSet optionSet)
213: {
214: Console.WriteLine("#SNMP is available at http://sharpsnmplib.codeplex.com");
215: Console.WriteLine("snmpget [Options] IP-address|host-name OID [OID] ...");
216: Console.WriteLine("Options:");
217: optionSet.WriteOptionDescriptions(Console.Out);
218: }
219:
220: private static IAuthenticationProvider GetAuthenticationProviderByName(string authentication, string phrase)
221: {
222: if (authentication.ToUpperInvariant() == "MD5")
223: {
224: return new MD5AuthenticationProvider(new OctetString(phrase));
225: }
226:
227: if (authentication.ToUpperInvariant() == "SHA")
228: {
229: return new SHA1AuthenticationProvider(new OctetString(phrase));
230: }
231:
232: throw new ArgumentException("unknown name", "authentication");
233: }
234: }
235: }
沒有留言:
張貼留言