I am trying to use the example from the wiki in a simple vb .net application. I am unable to communicate with the router. I get a host not found error.
My code is below.
Module Module1
'function from http://www.planet-source-code.com/vb/scripts/ShowCodeAsText.asp?txtCodeId=5179&lngWId=10
Private Function IPToInteger(ByVal Expression As String) As Integer
Try
Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(Expression)
With IPAddress
Return (System.Convert.ToInt32(.GetAddressBytes(3)) << 24) Or (System.Convert.ToInt32(.GetAddressBytes(2)) << 16) Or (System.Convert.ToInt32(.GetAddressBytes(1)) << Or System.Convert.ToInt32(.GetAddressBytes(0))
End With
Catch ex As Exception
Return 0I
End Try
End Function
Sub Main()
Dim l As Long
l = IPToInteger(“192.168.88.1”)
Dim mk = New Mikrotik(l)
If Not mk.Login(“admin”, “pwd”) Then
Console.WriteLine(“Cant log in”)
mk.Close()
Console.ReadLine()
Return
End If
mk.Send(“/system/clock/getall”, True)
For Each row In mk.Read()
Console.WriteLine(row)
Next
Console.ReadLine()
End Sub
End Module
I have not changed anything in the class from the wiki so am not posting that.
first things first:
check if API service port is enabled, it is not blocked by firewall
check if address of router is reachable from your PC
After that you can check with python client to see if everything seems to be good. After that you can start to check with some unknown client to try to connect, authenticate and then do some queries.
I suspect the problem is in my .net code. I do not have a dns server on my local network. This is a home network so I have modified the code to handle a single IP address.
API is different service and works on port 8728 (default).
Check this in ‘IP/Services’.
In your code IP address (as string) is converted to integer,
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
Sub New in Mikrotik Class requires ipOrDns As String.
There is also bug in ‘Function IPToInteger’, IP address is converted in reversed order, eg. 1.88.168.192.
I removed the ip address conversionfunction and now, I am getting an index out of range exception.
An unhandled exception of type ‘System.IndexOutOfRangeException’
occurred in RBCmd.exe
Additional information: Index was outside the bounds of the array.
The error is at
tcpCon.Connect(ips.AddressList(0), If(port = -1, 8728, port))
my modified code is
Sub Main()
Dim mk = New Mikrotik(“192.168.88.1”)
If Not mk.Login(“admin”, “pwd”) Then
Console.WriteLine(“Cant log in”)
mk.Close()
Console.ReadLine()
Return
End If
mk.Send(“/system/clock/getall”, True)
For Each row In mk.Read()
Console.WriteLine(row)
Next
Console.ReadLine()
End Sub
What I cannot fully understand is what the following line does.
Dim ips = Net.Dns.GetHostEntry(ipOrDns)
As far as I can tell, the above line is asking for a DNS server which I do not have. I just have one ip address to which I want the stream to connect to.
Sub Main()
Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse("192.168.88.1")
Dim mk = New Mikrotik(IPAddress)
If Not mk.Login("user", "pass") Then
Console.WriteLine("Cant log in")
mk.Close()
Console.ReadLine()
Return
End If
mk.Send("/system/clock/getall", True)
For Each row In mk.Read()
Console.WriteLine(row)
Next
Console.ReadLine()
End Sub
Public Sub New(ByVal IPAddr As System.Net.IPAddress, Optional ByVal port As Integer = -1)
tcpCon.Connect(IPAddr, If(port = -1, 8728, port))
tcpStream = tcpCon.GetStream()
End Sub