API in VB.NET

Hello everybody!

I need you help to resolve my problem.

My code :

Private Sub bt_ejecutar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_ejecutar.Click

Dim mk = New mkAPI(tb_ip.Text)
If Not mk.Login(user.Text, pass.Text) Then
mk.Close()
view.Text = "Erreur"
Exit Sub
End If

view.Text = "Connecté"
mk.Send("/ip/address/getall", False)
mk.Send("=.proplist=address", True)

For Each h As String In mk.Read()
TextBox1.Text &= h
Next

End Sub

routerboard answer: !re=address=192.168.10.70/24!done

I wouldlike remove all parameter and keep only ip address.

What is the solution please?

Thank you

But… you have already done it.

Everything else in the output you’re getting is just protocol demanded content. To strip it out, just… for each word, check if it starts with “=address=”, and if so, get the string after “=address=”. Check out the string functions.

Alternatively, get a different .NET client that would make things more convenient in this regard, such as this one perhaps.

Thank you for your answer.

I wouldlike print in my textbox only the IP address (192.168.10.70) and remove" !re=address=/24!done"

I don’t know functions on VB… I’m beginner. What is the function that I must use ?
I wouldlike keep the string after address= and before /24.

Thank you for your help

Being a beginner seems like the perfect reason to switch clients, while you’re still not used to one.

With the aforementioned client, you can do it like:

    Private Sub bt_ejecutar_Click(sender As Object, e As EventArgs) Handles bt_ejecutar.Click

        Dim mks = New TikSession(TikConnectorType.Api)
        Try
            mks.Open(tb_ip.Text, user.Text, pass.Text)
            view.Text = "Connecté"
        Catch ex As Exception
            view.Text = "Erreur"
        End Try

        For Each item As ITikEntityRow In mks.Connector.ExecuteReader(
            "/ip/address",
            ExecuteReaderBehaviors.None,
            New List(Of String) From {"address"}
        )
            TextBox1.Text &= item.GetStringValueOrNull("address", True)
        Next
    End Sub

Thank you for your answer

Ok, I will switch client.
I modify my source with your code, but (TikConnectorType.api, ITikEntityRow and ExecuteReaderBehaviors) are not accesible…

I use VB express 2010. Must I change software?
Or mùust I add library in my project? I read mikrotik4net’s website and I don’t find my answer…

Thank you for your help.

You download the archive from the website, and extract it somewhere (anywhere!) on your computer.

In Visual Studio, right click on the project, and select “Add Reference…”. Select “Browse”, and find the DLL from the archive you just extracted, and that’s pretty much it.

You could import the Tik4Net namespaces OR you could just manually type out “Tik4Net.” before the name of every class from that namespace.

Hello,

I’m trying to use the source “Tik4Net”, I imported namespace but I have an error.

“mks.Open” and “mks.Connector.ExecuteReader” are not member.

It’s strange.

I need your help. Thank You!

Are you sure TikSession was created properly? If you uncomment all lines within the Sub except the Dim one, does the program compile? What members are there?

Thank you for your answer.

Here is my source (Tiksession)

Imports System

Public Class TikSession

Dim tcpStream As IO.Stream
Dim tcpCon As New Net.Sockets.TcpClient

Public Sub New(ByVal ipOrDns As String, Optional ByVal port As Integer = -1)
Dim ips = Net.Dns.GetHostEntry(ipOrDns)

tcpCon.Connect(ips.AddressList(0), If(port = -1, 8728, port))
tcpStream = tcpCon.GetStream()
End Sub

Public Sub New(ByVal endP As System.Net.IPEndPoint)
tcpCon.Connect(endP)
tcpStream = tcpCon.GetStream()
End Sub

Public Sub Close()
tcpStream.Close()
tcpCon.Close()
End Sub

Public Function Login(ByVal user As String, ByVal pass As String) As Boolean
Send(“/login”, True)
Dim hash = Read()(0).Split(New String() {“ret=”}, StringSplitOptions.None)(1)
Send(“/login”)
Send(“=name=” + user)
Send(“=response=00” + EncodePassword(pass, hash), True)
Dim res = Read()
If (res(0) = “!done”) Then Return True Else Return False
End Function

Function EncodePassword(ByVal pass As String, ByVal challange As String) As String
Dim hash_byte(challange.Length / 2 - 1) As Byte
For i = 0 To challange.Length - 2 Step 2
hash_byte(i / 2) = Byte.Parse(challange.Substring(i, 2), Globalization.NumberStyles.HexNumber)
Next
Dim response(pass.Length + hash_byte.Length) As Byte
response(0) = 0
Text.Encoding.ASCII.GetBytes(pass.ToCharArray()).CopyTo(response, 1)
hash_byte.CopyTo(response, 1 + pass.Length)


Dim md5 = New System.Security.Cryptography.MD5CryptoServiceProvider()

Dim hash = md5.ComputeHash(response)

Dim hashStr As New Text.StringBuilder()
For Each h In hash
hashStr.Append(h.ToString(“x2”))
Next
Return hashStr.ToString()
End Function

Public Sub Send(ByVal command As String, Optional ByVal EndSentence As Boolean = False)
Dim bytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray())
Dim size = EncodeLength(bytes.Length)

tcpStream.Write(size, 0, size.Length)
tcpStream.Write(bytes, 0, bytes.Length)
If EndSentence Then tcpStream.WriteByte(0)
End Sub

Public Function Read() As List(Of String)
Dim output As New List(Of String)
Dim o = “”
Dim tmp(4) As Byte
Dim count As Long

While True
tmp(3) = tcpStream.ReadByte()
Select Case tmp(3)
Case 0
output.Add(o)
If o.Substring(0, 5) = “!done” Then
Exit While
Else
o = “”
Continue While
End If
Case Is < &H80
count = tmp(3)
Case Is < &HC0
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(3), 0, 0}, 0) ^ &H8000
Case Is < &HE0
tmp(2) = tcpStream.ReadByte()
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(2), tmp(3), 0}, 0) ^ &HC00000
Case Is < &HF0
tmp(2) = tcpStream.ReadByte()
tmp(1) = tcpStream.ReadByte()
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(1), tmp(2), tmp(3)}, 0) ^ &HE0000000
Case &HF0
tmp(3) = tcpStream.ReadByte()
tmp(2) = tcpStream.ReadByte()
tmp(1) = tcpStream.ReadByte()
tmp(0) = tcpStream.ReadByte()
count = BitConverter.ToInt32(tmp, 0)
Case Else
Exit While 'err
End Select

For i = 0 To count - 1
o += ChrW(tcpStream.ReadByte())
Next
End While
Return output
End Function

Function EncodeLength(ByVal l As Integer) As Byte()
If l < &H80 Then
Dim tmp = BitConverter.GetBytes(l)
Return New Byte() {tmp(0)}
ElseIf l < &H4000 Then
Dim tmp = BitConverter.GetBytes(l Or &H8000)
Return New Byte() {tmp(1), tmp(0)}
ElseIf l < &H200000 Then
Dim tmp = BitConverter.GetBytes(l Or &HC00000)
Return New Byte() {tmp(2), tmp(1), tmp(0)}
ElseIf l < &H10000000 Then
Dim tmp = BitConverter.GetBytes(l Or &HE0000000)
Return New Byte() {tmp(3), tmp(2), tmp(1), tmp(0)}
Else
Dim tmp = BitConverter.GetBytes(l)
Return New Byte() {&HF0, tmp(3), tmp(2), tmp(1), tmp(0)}
End If
End Function

End Class

For me, it’s good.
Thank you for your help

Wait… that’s not Tik4Net!

You’ve just renamed the VB class from the wiki to TikSession… That’s not going to work. You need to download the client from its Google code page. You know: this one here. Once you have it, add a reference from your project to that .DLL.

Hello Boen_Robot,

Sorry for the delay, i haven’t many time to continue my project.
I added .dll as you told me.

My problem is tiksession.
I need to create the source.
With the mikrotik’s Wiki, I find many library, but the communauty don’t give source. I understand that.

I need help to start.
My solution is someone accept to give me the source.

Can you help me?