0

再会,

我试图从Sharp-snmp 样本中找出这段代码。

https://github.com/lextudio/sharpsnmplib-samples/blob/master/Samples/VB.NET/snmpset/Program.vb

我正在使用 vb.net SET 示例。

Public Sub New(id As Lextm.SharpSnmpLib.ObjectIdentifier, data As Lextm.SharpSnmpLib.ISnmpData)
    Member of Lextm.SharpSnmpLib.Variable

Public Sub New(id As UInteger(), data As Lextm.SharpSnmpLib.ISnmpData)
    Member of Lextm.SharpSnmpLib.Variable

我的 Name 语法是错误的,还是它必须是 OID 整数?当我使用它运行的 OID 时,当我使用它的名称时它就死了。

System_Operation_Mode.0 'name
1.3.6.1.4.1.21703.100.1.1.0 'oid

这是 SET 示例的一部分,它在代码底部的 asterix ** 处死亡。Extra(i) 填写的是上面的 Name 而不是 OID。

    ReDim args(3)
        args(0) = "192.168.45.5" 'IP Address
        args(1) = NetworkConfig.addrModeStringCommand 'command string name System_Operation_Mode.0
        args(2) = "i" 'tell it what data type you are sending
        args(3) = NetworkConfig.StaticMode 'mode you want from the IPNetwork, in this case static


        Dim p As OptionSet = New OptionSet().Add("c:", "Community name, (default is public)", Sub(v As String)
                                                                                                  If v IsNot Nothing Then
                                                                                                      community = v
                                                                                                  End If
                                                                                              End Sub) _
                                            .Add("l:", "Security level, (default is noAuthNoPriv)", Sub(v As String)
                                                                                                        If v.ToUpperInvariant() = "NOAUTHNOPRIV" Then
                                                                                                            level = Levels.Reportable
                                                                                                        ElseIf v.ToUpperInvariant() = "AUTHNOPRIV" Then
                                                                                                            level = Levels.Authentication Or Levels.Reportable
                                                                                                        ElseIf v.ToUpperInvariant() = "AUTHPRIV" Then
                                                                                                            level = Levels.Authentication Or Levels.Privacy Or Levels.Reportable
                                                                                                        Else
                                                                                                            Throw New ArgumentException("no such security mode: " & v)
                                                                                                        End If
                                                                                                    End Sub) _
                                            .Add("a:", "Authentication method (MD5 or SHA)", Sub(v As String)
                                                                                                 authentication = v
                                                                                             End Sub) _
                                            .Add("A:", "Authentication passphrase", Sub(v As String)
                                                                                        authPhrase = v
                                                                                    End Sub) _
                                            .Add("x:", "Privacy method", Sub(v As String)
                                                                             privacy = v
                                                                         End Sub) _
                                            .Add("X:", "Privacy passphrase", Sub(v As String)
                                                                                 privPhrase = v
                                                                             End Sub) _
                                            .Add("u:", "Security name", Sub(v As String)
                                                                            user = v
                                                                        End Sub) _
                                            .Add("C:", "Context name", Sub(v As String)
                                                                           contextName = v
                                                                       End Sub) _
                                            .Add("h|?|help", "Print this help information.", Sub(v As String)
                                                                                                 showHelp__1 = v IsNot Nothing
                                                                                             End Sub) _
                                            .Add("V", "Display version number of this application.", Sub(v As String)
                                                                                                         showVersion = v IsNot Nothing
                                                                                                     End Sub) _
                                            .Add("d", "Display message dump", Sub(v As String)
                                                                                  dump = True
                                                                              End Sub) _
                                            .Add("t:", "Timeout value (unit is second).", Sub(v As String)
                                                                                              timeout = Integer.Parse(v) * 1000
                                                                                          End Sub) _
                                            .Add("r:", "Retry count (default is 0)", Sub(v As String)
                                                                                         retry = Integer.Parse(v)
                                                                                     End Sub) _
                                            .Add("v:", "SNMP version (1, 2, and 3 are currently supported)", Sub(v As String)
                                                                                                                 Select Case Integer.Parse(v)
                                                                                                                     Case 1
                                                                                                                         version = VersionCode.V1
                                                                                                                         Exit Select
                                                                                                                     Case 2
                                                                                                                         version = VersionCode.V2
                                                                                                                         Exit Select
                                                                                                                     Case 3
                                                                                                                         version = VersionCode.V3
                                                                                                                         Exit Select
                                                                                                                     Case Else
                                                                                                                         Throw New ArgumentException("no such version: " & v)
                                                                                                                 End Select
                                                                                                             End Sub)

        If args.Length = 0 Then
            ShowHelp(p)
            Return
        End If

        Dim extra As List(Of String)
        Try
            extra = p.Parse(args)
        Catch ex As OptionException
            Console.WriteLine(ex.Message)
            Return
        End Try

        If showHelp__1 Then
            ShowHelp(p)
            Return
        End If

        If (extra.Count - 1) Mod 3 <> 0 Then
            Console.WriteLine("invalid variable number: " & extra.Count)
            Return
        End If

        If showVersion Then
            Console.WriteLine(Reflection.Assembly.GetExecutingAssembly().GetName().Version)
            Return
        End If

        Dim ip As IPAddress
        Dim parsed As Boolean = IPAddress.TryParse(extra(0), ip)
        If Not parsed Then
            For Each address As IPAddress In Dns.GetHostAddresses(extra(0))
                If address.AddressFamily <> AddressFamily.InterNetwork Then
                    Continue For
                End If

                ip = address
                Exit For
            Next

            If ip Is Nothing Then
                Console.WriteLine("invalid host or wrong IP address found: " & extra(0))
                Return
            End If
        End If

        Try
            Dim vList As New List(Of Variable)()
            Dim i As Integer = 1
            While i < extra.Count
                Dim type As String = extra(i + 1)
                If type.Length <> 1 Then
                    Console.WriteLine("invalid type string: " & type)
                    Return
                End If

                Dim data As ISnmpData

                Select Case type(0)
                    Case "i"c
                        data = New Integer32(Integer.Parse(extra(i + 2)))
                        Exit Select
                    Case "u"c
                        data = New Gauge32(UInteger.Parse(extra(i + 2)))
                        Exit Select
                    Case "t"c
                        data = New TimeTicks(UInteger.Parse(extra(i + 2)))
                        Exit Select
                    Case "a"c
                        data = New IP(IPAddress.Parse(extra(i + 2)).GetAddressBytes())
                        Exit Select
                    Case "o"c
                        data = New ObjectIdentifier(extra(i + 2))
                        Exit Select
                    Case "x"c
                        data = New OctetString(ByteTool.Convert(extra(i + 2)))
                        Exit Select
                    Case "s"c
                        data = New OctetString(extra(i + 2))
                        Exit Select
                    Case "d"c
                        data = New OctetString(ByteTool.ConvertDecimal(extra(i + 2)))
                        Exit Select
                    Case "n"c
                        data = New Null()
                        Exit Select
                    Case Else
                        Console.WriteLine("unknown type string: " & type(0))
                        Return
                End Select

                Dim test As New Variable(*New ObjectIdentifier(extra(i))*, data)
                vList.Add(test)
                i = i + 3
            End While

我一直在使用“名称”而不是“OID”有没有办法改变它可以读取,或者将它们转换?还是我必须回去使用 OID?

4

0 回答 0