Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get
Creating a function to condense SMNP get Creating a function to condense SMNP get
Creating a function to condense SMNP get
Go Back  Xtreme Visual Basic Talk > > > Creating a function to condense SMNP get


Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2014, 10:46 PM
FrogFumes FrogFumes is offline
Newcomer
 
Join Date: May 2014
Posts: 4
Default Creating a function to condense SMNP get


Hello all,

Excited to be a part of a new forum.

I am not a programmer by trade but play my hand at it when the need arises. I have worked with VB6 mostly and I am finally trying to wrap my head around OOP.

I work with some gear which has atrocious UI and decided to write a program which can pull the relevant information I need vis SNMP without pouring through tons of menus.

That being said I discovered a nice dll written by the folks over a sharpsnmp.


There VB.NET example is here
http://www.snmpsharpnet.com/?page_id=105

WHat I would like to do is insert the Get command into a function so that each time I need to use it I can just pass the function and some arguments without retyping the whole code each time.

For example.

Code:
Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        Dim host As String = "localhost"
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If
    End Sub
 
End Module
Is the example given and I would like the function to operate with arguments for the string for host and the string for OID.

The problem I am running into is im not sure what return value I need to place for the function.

so If I want it to be

Code:
Public Function GetSNMP(byval HostAddress as string, Byval OID as string) as ReturnValue???
Essentially what I am wanting to do is connect to a host pull various integers by passing the get request to different OID's and parse the returned kvp.key.tostring() and kvp.value.tostring() into listviews.

My attempt so far has led me to.

Code:
 Imports SnmpSharpNet

Module GetSNMP


    Public Function GetSNMP(ByVal HostAddress As String, ByVal OID As String) As String '<~~~ Passing as a string is not correct
        Dim host As String = HostAddress
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {OID}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Function
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If

    End Function

End Module
Again, I'd like to be able to pass this by using a function but am unsure as how to go about it and be able to get the information.

My current use is placed under a button as a proof of concept but is a bit lengthy and I would like to reduce the redundant code.

Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        lvDetails2.Items.Clear()

        Dim host As String = ComboBox1.Text
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        Dim rootOid As Oid = New Oid(".1.3.6.1.4.1.15921.1.3.3.1.1.11")
        Dim nextOid As Oid = rootOid
        Dim keepGoing As Boolean = True
        requestOid = New String() {rootOid.ToString()}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        While keepGoing
            result = snmp.GetNext(SnmpVersion.Ver1, New String() {nextOid.ToString()})
            If result IsNot Nothing Then
                Dim kvp As KeyValuePair(Of Oid, AsnType)
                For Each kvp In result
                    If rootOid.IsRootOf(kvp.Key) Then
                        Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                              SnmpConstants.GetTypeName(kvp.Value.Type), _
                                              kvp.Value.ToString())
                        Dim btsID As String = kvp.Key.ToString()
                        Dim dlPIN As String = kvp.Value.ToString()
                        Dim objItem As New ListViewItem

                        If btsID.Contains("1.1.11.1.1.1") Then
                            ListBox1.Items.Add("RIU 1 LTE DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "1"
                                .SubItems.Add("700")
                                .SubItems.Add("A")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.2") Then
                            ListBox1.Items.Add("RIU 1 Cell A DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "2"
                                .SubItems.Add("Cell A")
                                .SubItems.Add("n/a")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.3") Then
                            ListBox1.Items.Add("RIU 1 PCS DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "3"
                                .SubItems.Add("PCS")
                                .SubItems.Add("n/a")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.1.4") Then
                            ListBox1.Items.Add("RIU 1 AWS DL Input = " & dlPIN - 100)
                            With objItem
                                .Text = "4"
                                .SubItems.Add("AWS")
                                .SubItems.Add("A")
                                .SubItems.Add(dlPIN - 100)
                            End With
                            lvDetails2.Items.Add(objItem)
                        End If
                        If btsID.Contains("1.1.11.1.2.1") Then
                            ListBox1.Items.Add("RIU 2 LTE DL Input = " & dlPIN - 100)
                        End If
                        If btsID.Contains("1.1.11.1.2.2") Then
                            ListBox1.Items.Add("RIU 2 Cell B DL Input = " & dlPIN - 100)
                        End If
                        If btsID.Contains("1.1.11.1.2.3") Then
                            ListBox1.Items.Add("RIU 2 AWS DL Input = " & dlPIN - 100)
                        End If
                        nextOid = kvp.Key
                    Else
                        keepGoing = False
                    End If
                Next
            Else
                Console.WriteLine("No results received.")
                keepGoing = False
            End If
        End While
End Sub
Thanks in advance for any pointers that can be offered.
Reply With Quote
  #2  
Old 05-22-2014, 03:50 AM
PlausiblyDamp's Avatar
PlausiblyDampCreating a function to condense SMNP get PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Have you tried Dictionary(Of Oid, AsnType)? That looks like the result type in the original snippet so it might make most sense here.

If not I might have more time to take a proper look at this later.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-22-2014, 08:26 AM
FrogFumes FrogFumes is offline
Newcomer
 
Join Date: May 2014
Posts: 4
Default

Thanks for the suggestion Mr. PlausiblyDamp.

You may have overestimated my ability at coding.

When you suggest Dictionary(of oID, ASN type) where are you suggesting that is placed.

Looking over some examples is seems that dictionary functions like an array but an array set as a returnvalue.

If I were to write the keys and values to them would they write sequentially to the dictionary as

dictionary(0)
dictionary(1)
dictionary(2)


etc..

So my function would head as

Code:
Public Function GetSnmp(byval HostAddress as String, byval OID as string) as dictionary
Later I would include

Code:
Dim btsID As String = kvp.Key.ToString()
          Dim dlPIN As String = kvp.Value.ToString()
                For Each kvp In result
                    If rootOid.IsRootOf(kvp.Key) Then
                        dictionary.add(btsid)
                        dictionary.add(dlPIN)
next
Thanks for your guidance.
Reply With Quote
  #4  
Old 05-26-2014, 10:11 AM
PlausiblyDamp's Avatar
PlausiblyDampCreating a function to condense SMNP get PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are just looking at returning the SNMP information then the function would probably be something like
Code:
Public Function GetSNMP(ByVal HostAddress As String, ByVal OID As String) As Dictionary(Of Oid, AsnType)
        Dim host As String = HostAddress
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {OID}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Function
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)

        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If
        Return result

End Function
I have pretty much kept the code you included, however you probably don't need all the Console.WriteLine(...) stuff in there as that looks like it is just printing diagnostic information rather than doing anything useful.

A Dictionary (http://msdn.microsoft.com/en-us/libr...code-snippet-1) is a collection where every item has an associated key - in the case of the posted code the key would be of type Oid and the value would be of AsnType.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get Creating a function to condense SMNP get
Creating a function to condense SMNP get
Creating a function to condense SMNP get
 
Creating a function to condense SMNP get
Creating a function to condense SMNP get
 
-->