PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/VB.NET/snmpwalk/Program.vb

#
Visual Basic | 216 lines | 190 code | 18 blank | 8 comment | 1 complexity | 49a82f765334985778ac7e35b532dfc9 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. '
  2. ' Created by SharpDevelop.
  3. ' User: Administrator
  4. ' Date: 2010/4/25
  5. ' Time: 14:14
  6. '
  7. ' To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. '
  9. Imports Lextm.SharpSnmpLib
  10. Imports Lextm.SharpSnmpLib.Security
  11. Imports Lextm.SharpSnmpLib.Messaging
  12. Imports Mono.Options
  13. Imports System.Net
  14. Imports System.Net.Sockets
  15. Module Program
  16. Sub Main(ByVal args As String())
  17. Dim community As String = "public"
  18. Dim dump As Boolean = True
  19. Dim showHelp__1 As Boolean = False
  20. Dim showVersion As Boolean = False
  21. Dim version As VersionCode = VersionCode.V1
  22. Dim timeout As Integer = 1000
  23. Dim retry As Integer = 0
  24. Dim maxRepetitions As Integer = 10
  25. Dim level As Levels = Levels.Reportable
  26. Dim user As String = String.Empty
  27. Dim authentication As String = String.Empty
  28. Dim authPhrase As String = String.Empty
  29. Dim privacy As String = String.Empty
  30. Dim privPhrase As String = String.Empty
  31. Dim mode As WalkMode = WalkMode.WithinSubtree
  32. Dim p As OptionSet = New OptionSet().Add("c:", "Community name, (default is public)", Sub(v As String)
  33. If v IsNot Nothing Then
  34. community = v
  35. End If
  36. End Sub) _
  37. .Add("l:", "Security level, (default is noAuthNoPriv)", Sub(v As String)
  38. If v.ToUpperInvariant() = "NOAUTHNOPRIV" Then
  39. level = Levels.Reportable
  40. ElseIf v.ToUpperInvariant() = "AUTHNOPRIV" Then
  41. level = Levels.Authentication Or Levels.Reportable
  42. ElseIf v.ToUpperInvariant() = "AUTHPRIV" Then
  43. level = Levels.Authentication Or Levels.Privacy Or Levels.Reportable
  44. Else
  45. Throw New ArgumentException("no such security mode: " & v)
  46. End If
  47. End Sub) _
  48. .Add("a:", "Authentication method (MD5 or SHA)", Sub(v As String)
  49. authentication = v
  50. End Sub) _
  51. .Add("A:", "Authentication passphrase", Sub(v As String)
  52. authPhrase = v
  53. End Sub) _
  54. .Add("x:", "Privacy method", Sub(v As String)
  55. privacy = v
  56. End Sub) _
  57. .Add("X:", "Privacy passphrase", Sub(v As String)
  58. privPhrase = v
  59. End Sub) _
  60. .Add("u:", "Security name", Sub(v As String)
  61. user = v
  62. End Sub) _
  63. .Add("h|?|help", "Print this help information.", Sub(v As String)
  64. showHelp__1 = v IsNot Nothing
  65. End Sub) _
  66. .Add("V", "Display version number of this application.", Sub(v As String)
  67. showVersion = v IsNot Nothing
  68. End Sub) _
  69. .Add("d", "Display message dump", Sub(v As String)
  70. dump = True
  71. End Sub) _
  72. .Add("t:", "Timeout value (unit is second).", Sub(v As String)
  73. timeout = Integer.Parse(v) * 1000
  74. End Sub) _
  75. .Add("r:", "Retry count (default is 0)", Sub(v As String)
  76. retry = Integer.Parse(v)
  77. End Sub) _
  78. .Add("v|version:", "SNMP version (1, 2, and 3 are currently supported)", Sub(v As String)
  79. Select Case Integer.Parse(v)
  80. Case 1
  81. version = VersionCode.V1
  82. Exit Select
  83. Case 2
  84. version = VersionCode.V2
  85. Exit Select
  86. Case 3
  87. version = VersionCode.V3
  88. Exit Select
  89. Case Else
  90. Throw New ArgumentException("no such version: " & v)
  91. End Select
  92. End Sub) _
  93. .Add("m|mode:", "WALK mode (subtree, all are supported)", Sub(v As String)
  94. If v = "subtree" Then
  95. mode = WalkMode.WithinSubtree
  96. ElseIf v = "all" Then
  97. mode = WalkMode.[Default]
  98. Else
  99. Throw New ArgumentException("unknown argument: " & v)
  100. End If
  101. End Sub) _
  102. .Add("Cr:", "Max-repetitions (default is 10)", Sub(v As String)
  103. maxRepetitions = Integer.Parse(v)
  104. End Sub)
  105. If args.Length = 0 Then
  106. ShowHelp(p)
  107. Return
  108. End If
  109. Dim extra As List(Of String)
  110. Try
  111. extra = p.Parse(args)
  112. Catch ex As OptionException
  113. Console.WriteLine(ex.Message)
  114. Return
  115. End Try
  116. If showHelp__1 Then
  117. ShowHelp(p)
  118. Return
  119. End If
  120. If extra.Count < 1 OrElse extra.Count > 2 Then
  121. Console.WriteLine("invalid variable number: " & extra.Count)
  122. Return
  123. End If
  124. If showVersion Then
  125. Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version)
  126. Return
  127. End If
  128. Dim ip As IPAddress
  129. Dim parsed As Boolean = IPAddress.TryParse(extra(0), ip)
  130. If Not parsed Then
  131. For Each address As IPAddress In Dns.GetHostAddresses(extra(0))
  132. If address.AddressFamily <> AddressFamily.InterNetwork Then
  133. Continue For
  134. End If
  135. ip = address
  136. Exit For
  137. Next
  138. If ip Is Nothing Then
  139. Console.WriteLine("invalid host or wrong IP address found: " & extra(0))
  140. Return
  141. End If
  142. End If
  143. Try
  144. Dim test As ObjectIdentifier = If(extra.Count = 1, New ObjectIdentifier("1.3.6.1.2.1"), New ObjectIdentifier(extra(1)))
  145. Dim result As IList(Of Variable) = New List(Of Variable)()
  146. Dim receiver As New IPEndPoint(ip, 161)
  147. If version = VersionCode.V1 Then
  148. Messenger.Walk(version, receiver, New OctetString(community), test, result, timeout, _
  149. mode)
  150. ElseIf version = VersionCode.V2 Then
  151. Messenger.BulkWalk(version, receiver, New OctetString(community), test, result, timeout, _
  152. maxRepetitions, mode, Nothing, Nothing)
  153. Else
  154. If String.IsNullOrEmpty(user) Then
  155. Console.WriteLine("User name need to be specified for v3.")
  156. Return
  157. End If
  158. Dim auth As IAuthenticationProvider
  159. If (level And Levels.Authentication) = Levels.Authentication Then
  160. auth = GetAuthenticationProviderByName(authentication, authPhrase)
  161. Else
  162. auth = DefaultAuthenticationProvider.Instance
  163. End If
  164. Dim priv As IPrivacyProvider
  165. If (level And Levels.Privacy) = Levels.Privacy Then
  166. priv = New DESPrivacyProvider(New OctetString(privPhrase), auth)
  167. Else
  168. priv = New DefaultPrivacyProvider(auth)
  169. End If
  170. Dim report As ReportMessage = Messenger.NextDiscovery.GetResponse(timeout, receiver)
  171. Messenger.BulkWalk(version, receiver, New OctetString(user), test, result, timeout, _
  172. maxRepetitions, mode, priv, report)
  173. End If
  174. For Each variable As Variable In result
  175. Console.WriteLine(variable)
  176. Next
  177. Catch ex As SnmpException
  178. Console.WriteLine(ex)
  179. Catch ex As SocketException
  180. Console.WriteLine(ex)
  181. End Try
  182. End Sub
  183. Private Function GetAuthenticationProviderByName(ByVal authentication As String, ByVal phrase As String) As IAuthenticationProvider
  184. If authentication.ToUpperInvariant() = "MD5" Then
  185. Return New MD5AuthenticationProvider(New OctetString(phrase))
  186. End If
  187. If authentication.ToUpperInvariant() = "SHA" Then
  188. Return New SHA1AuthenticationProvider(New OctetString(phrase))
  189. End If
  190. Throw New ArgumentException("unknown name", "authentication")
  191. End Function
  192. Private Sub ShowHelp(ByRef optionSet As OptionSet)
  193. Console.WriteLine("#SNMP is available at http://sharpsnmplib.codeplex.com")
  194. Console.WriteLine("snmpwalk [Options] IP-address|host-name [OID]")
  195. Console.WriteLine("Options:")
  196. optionSet.WriteOptionDescriptions(Console.Out)
  197. End Sub
  198. End Module