Click here to Skip to main content
16,005,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am sending SNMP SystemName query to local host and in response i m getting little square characters instead of the specific system name..

Device SNMP Information:
sysName - Datatype: 48, [][][][]

Here is the code

VB
Imports System.Text

Class Form1

    Dim commlength As Integer, miblength As Integer, datatype As Integer, datalength As Integer, datastart As Integer
    Dim uptime As Integer
    Dim output As String
    Dim conn As New SNMP()
    Dim response As Byte() = New Byte(1023) {}
    'Public Shared Sub Main(ByVal argv As String())
    'End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IPaddress.Text = "115.186.115.188"
        Community.Text = "public"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ListBox1.Items.Add("Device SNMP information:")

        ' Send sysName SNMP request
        response = conn.[get]("get", IPaddress.Text, Community.Text, "1.3.6.1.2.1.1.5.0")
        If response(0) = &HFF Then
            ListBox1.Items.Add("No response from " & IPaddress.Text)
            Return
        End If

        ' If response, get the community name and MIB lengths
        commlength = Convert.ToInt16(response(6))
        miblength = Convert.ToInt16(response(23 + commlength))

        ' Extract the MIB data from the SNMP response
        datatype = Convert.ToInt16(response(24 + commlength + miblength))
        datalength = Convert.ToInt16(response(25 + commlength + miblength))
        datastart = 26 + commlength + miblength
        output = Encoding.ASCII.GetString(response, datastart, datalength)
        ListBox1.Items.Add("  sysName - Datatype:" & datatype & "," & output)
Posted

Change your encoding from ASCII to UTF8 or Unicode and see what you get.

C#
output = Encoding.UTF8.GetString(response, datastart, datalength)
 
Share this answer
 
I m getting same boxes by replacing Ascii with UTF8 ... and I m getting some unrecognizable characters when I wrote unicode in place of Ascii
 
Share this answer
 
Comments
André Kraak 9-Oct-11 16:59pm    
If you have a question about or comment on a given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.

Please move the content of this solution to the solution you are commenting on and remove the solution.
Thank you.
I'm not sure why you are writing to the listbox 3 different times with 3 different items, but on the bottom there is this code

VB
ListBox1.Items.Add("  sysName - Datatype:" & datatype & "," & output)

Will doing this help
Adding the "ToString"
VB
ListBox1.Items.Add("  sysName - Datatype:" & datatype.ToString & "," & output)
 
Share this answer
 
v2
The problem after looking again appears to be in how the last output is built so my adding the to string wouldn't make a difference. It is properly displaying the Given String, then the dataype appears to be 48 the it list the comma "," but then it goes wrong.
VB
datastart = (26 + commlength + miblength) 'possibly add () then

Dim NOP as new stringbuilder
NOP.append(response.ToString)  'May need to add the .ToString on the end of each.
NOP.append(datastart)
NOP.append(datalength)

dim output as string

output = NOP     ' Might need to add a .ToString here also
ListBox1.Items.Add("  sysName - Datatype:" & datatype & "," & output)
 
Share this answer
 
Comments
Madiha Ahmed 12-Oct-11 15:39pm    
@ ledtech ...
Now I m getting response of some of the OIDs (System Name, System up time and System contact) but for some of the OIDs (like system description, System Date) I m getting same square characters..it seems like my prog is not supporting some of the data types..
ledtech3 12-Oct-11 21:28pm    
What version of VB are you building with? I'm trying to build this program and step thru the code but i'm not real familar with SNMP protocol researching it now.But the line that says Dim conn As New SNMP() has the red squigly in VS 2008 so not sure what to reference for that,already tried ,Imports System.Net,
Imports System.Management
Besides that if you used my code the we are only adding 3 items , you would have to create more to return others.
ledtech3 13-Oct-11 3:25am    
I found more of your code on msdn while researching this problem.If i'm understanding this correctly then you are only getting back 3 propertys to start with, of the item you are connecting to.So you may need to find out what to connect to to get the other propertys you want returned.
It could also possibly the version that the system is using,like if the newer version supports more propertys.I ran across a referance that stated older versions returned all or nothing. The information on the problem is to general without looking at the the full results and how the full program works.
Hope I've been of some help to you.
Madiha Ahmed 13-Oct-11 10:22am    
I'm also using vb 2008...
just add this class to your program after converting to vb and set your Main form program as startup project ..then you will get rid of that red squigy line..
Madiha Ahmed 13-Oct-11 10:26am    
coming back to the problem..I m thinking to use Select Case statements for the data type that SNMP uses ..so that when the response comes I can categorize them according to their data types because right now my program is giving response only for some of the datatypes..and for the other it is giving square characters!
Here is the class, see if you missed any of the fixes.Besides the change you had there were a few others here i had to make also.
my main form imports:
Imports System.Text
Imports System.Net
Imports System.Management (Also had to add the reference to System.Management)
Imports TestAp1.SNMP (which is the class)

VB
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Class SNMP
    Public Sub New()
    End Sub
    Public Function [get](ByVal request As String, ByVal host As String, ByVal community As String, ByVal mibstring As String) As Byte()
        Dim packet As Byte() = New Byte(1023) {}
        Dim mib As Byte() = New Byte(1023) {}
        Dim snmplen As Integer
        Dim comlen As Integer = community.Length
        Dim mibvals As String() = mibstring.Split("."c)
        Dim miblen As Integer = mibvals.Length
        Dim cnt As Integer = 0, temp As Integer, i As Integer
        Dim orgmiblen As Integer = miblen
        Dim pos As Integer = 0 - 1
        ' Convert the string MIB into a byte array of integer values
        ' Unfortunately, values over 128 require multiple bytes
        ' which also increases the MIB length
        For i = 0 To orgmiblen - 1
            temp = Convert.ToInt16(mibvals(i))
            If temp > 127 Then
                mib(cnt) = Convert.ToByte(128 + (temp \ 128))
                mib(cnt + 1) = Convert.ToByte(temp - ((temp \ 128) * 128))
                cnt += 2
                miblen += 1
            Else
                mib(cnt) = Convert.ToByte(temp)
                cnt += 1
            End If
        Next
        snmplen = 29 + comlen + miblen - 1
        'Length of entire SNMP packet
        'The SNMP sequence start
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30
        'Sequence start
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(snmplen - 2)
        'sequence size
        'SNMP version
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2
        'Integer type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1
        'length
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        'SNMP version 1
        'Community name
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H4
        ' String type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(comlen)
        'length
        'Convert community name to byte array
        Dim data As Byte() = Encoding.ASCII.GetBytes(community)
        For i = 0 To data.Length - 1
            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = data(i)
        Next
        'Add GetRequest or GetNextRequest value
        If request = "get" Then
            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &HA0
        Else
            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &HA1
        End If
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(20 + miblen - 1)
        'Size of total MIB
        'Request ID
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2
        'Integer type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H4
        'length
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        'SNMP request ID
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1
        'Error status
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2
        'Integer type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1
        'length
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        'SNMP error status
        'Error index
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2
        'Integer type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H1
        'length
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        'SNMP error index
        'Start of variable bindings
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30
        'Start of variable bindings sequence
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(6 + miblen - 1)
        ' Size of variable binding
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H30
        'Start of first variable bindings sequence
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(6 + miblen - 1 - 2)
        ' size
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H6
        'Object type
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(miblen - 1)
        'length
        'Start of MIB
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H2B
        'Place MIB array in packet
        For i = 2 To miblen - 1
            packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = Convert.ToByte(mib(i))
        Next
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H5
        'Null object value
        packet(System.Math.Max(System.Threading.Interlocked.Increment(pos), pos - 1)) = &H0
        'Null
        'Send packet to destination
        Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000)
        Dim ihe As IPHostEntry = Dns.GetHostEntry(host)
        Dim iep As New IPEndPoint(ihe.AddressList(0), 161)
        Dim ep As EndPoint = DirectCast(iep, EndPoint)
        sock.SendTo(packet, snmplen, SocketFlags.None, iep)
        'Receive response from packet
        Try
            Dim recv As Integer = sock.ReceiveFrom(packet, ep)
        Catch generatedExceptionName As SocketException
            packet(0) = &HFF
        End Try
        Return packet
    End Function
End Class
 
Share this answer
 
v2
Comments
Madiha Ahmed 15-Oct-11 7:38am    
yea I got your zip file..
m thinking to use If then statements for the data types..
Thank u so much for your time..
ledtech3 15-Oct-11 12:17pm    
Your Original post returned a data type of 48 which the equiptment that i checked did not contain a data type of 48.
Hope my version can shed some light on why you are getting the brackets and i wasnt.Hope iv'e been of some help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900