API problem with VB.net Example

Hello,

I’ve been messing with a program to log into a router and pull some information over the API. I’ve been using the VB.net example listed on the wiki here: http://wiki.mikrotik.com/wiki/API_in_VB_dot_NET

This works great for most of the things I’ve done. But with specific things, mostly ones that return a big amount of information, the code dies out. I’ve narrowed it down to this section of the code:

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

Specifically, everything works when it is using this part (a small amount of data):

Case Is < &H80
					count = tmp(3)

But I get an error if it makes it to this part:

				Case Is < &HC0
					count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(3), 0, 0}, 0) ^ &H8000

This part of the code is actually setting count = “Infinity”, so I’m sure thats not correct.

I’ve posted in some VB.Net forums and been trying to figure out exactly what this is doing but I’ve had no luck. If any of you have tackeled this or know where to point me I’d be very greatfull. Thanks!

–Usogi

Have you considered using a different .NET client? Like maybe this one.

Sure, it’s written in C#, but that’s the beauty of the .NET framework - compile it, take the binary, and use it within VB.NET.

wow, thats pretty nice. Thank you very much for the info

Just for anyone else that using vb.net and wants to use this, heres a quick, simple, example

        Using session As New TikSession(TikConnectorType.Api)
            session.Open("192.168.88.10", "admin", "password")
            Dim api As Connector.Api.IApiConnector = session.Connector
            Dim result = api.ApiExecuteReader("/system/identity/print")
            MsgBox(result(0).GetStringValueOrNull("name", True))
            
        End Using

If there are multiple results, like if you send a /interface/ethernet/print they get put in result(0), result(1), and result(2).

then if you change “name” to something like “mac-address”, or “.id” it will give you that value. Pretty handy stuff!

How would you pass an argument to an example like the above using vb.net, since where is not a valid api command?

/ip/add/print/where comment=“somecomment”

Getting closer but still have errors:

Target returns error.
COMMAND:
/ip/arp/print
=.proplist=mac-address
=?address=x.x.x.x
ERRORS:
=message=unknown parameter
RESPONSE:
!trap
=message=unknown parameter
!done

Can someone verify my syntax for API commands are correct? (ip address is commented out)

I don’t see an example of a simple API read with query parameter for the mikrotik4tik connector, would like to build onto the above example that was posted above so others have a reference.
Thanks-

/ip/arp/print
=.proplist=mac-address
=?address=x.x.x.x

you should change to this

/ip/arp/print
=.proplist=mac-address
?address=x.x.x.x

That’s how I’m sending it from my program. The output I pasted was from the debug when executing the code. Maybe I’m not invoking the right function ExecuteReader() from the miktotik4tik class.

Anyone have experience with this API implementation?

Thanks for pointing out the correct API commands so I’m not chasing the wrong issue.

Here is the code:

Using session As New TikSession(TikConnectorType.Api)
session.Open(“mikrotikip”, “user”, “password”)
Dim api As Connector.Api.IApiConnector = session.Connector
Dim params As New Dictionary(Of String, String)
params.Add(“.proplist”, “mac-address”)
params.Add(“?address”, “x.x.x.x”)
Dim result As Object = api.ApiExecuteReader(“/ip/arp/print”, params)
Me.txtMacAddress.Text = (result(0).GetStringValueOrNull(“mac-address”, True))
End Using

Fixed -

Formatting of propertylist items and filter items are set by mikrotik4net api client and it also adds the ‘print’ statement.

Example query using vb.net with added filter query:

Using session As New TikSession(TikConnectorType.Api)
Dim filter As New TikConnectorQueryFilterDictionary
filter.Add(“address”, “x.x.x.x”)
Dim propertylist As New List(Of String)
propertylist.Add(“mac-address”)
session.Open(“MikrotikIP”, “user”, “pass”)
Dim api As Connector.Api.IApiConnector = session.Connector
Dim result As Object = api.ExecuteReader(“/ip/arp”, ExecuteReaderBehaviors.None, (propertylist), Filter)
Me.txtMacAddress.Text = (result(0).GetStringValueOrNull(“mac-address”, True))
End Using

Dan-