Another RouterOS API Delphi Client. Supports API over TLS and RouterOS 6.43+ API login method.
Official API documentation: https://wiki.mikrotik.com/wiki/Manual:API
MikroTik Forum topic: https://forum.mikrotik.com/viewtopic.php?f=9&t=31555
This is an implementation of MikroTik RouterOS API Client for Delphi. It supports execution of parallel requests to a router and has database-like interface for ease of use.
RouterOSAPI unit contains definition of two classes which you need to work with API protocol from your Delphi programs.
This class encapsulates properties and methods to make a connection to a router via RouterOS API protocol.
This function connects to the router and performs login procedure. It returns True if login was successful, False otherwise.
Makes a query to the router. Request is an array of strings, first one being the command and others are parameters. If GetAllAfterQuery is True, then TROSAPIResult.GetAll is executed after sending a query.
If you do not need to receive any output from your query, use this method. It simply calls Query function and frees returned object.
With this property you can set timeout value for network operations (in milliseconds).
This read-only property contains textual description of the last error occured.
Disconnects from the router.
This class gives you an ability to work with data returned from queries. Each command execution is "isolated" in its TRosApiResult object, so you can do parallel requests by calling TRosApiClient.Query and receiving several TRosApiResult objects.
Returns the value of Name parameter (word in terms of API) in current sentence. The preferred way of getting the result is the following: ApiResult['ParmName'] instead of ApiResult.ValueByName('ParmName'). You can use param name both with and without leading '=' character (ApiResult['address'] and ApiResult['=address'] return the same result).
Returns current sentence of query result (type is TRosApiSentence).
Receives one sentence from the router. If Wait parameter is True, function will wait until sentence is received. If Wait is False and no sentences were received for now, function returns False. This is helpful when executing infinite commands (like 'listen') in GUI, when you need to process other user's actions: you should periodically call GetOne with Wait = False, and in case of negative result just do something else for a time.
Receives all sentences upto '!done', then returns True (or False in case of a timeout).
Returns number of received sentences after calling GetAll.
Returns True if there's more sentence(s) in query result.
Returns True if there were trap(s) during GetAll
Returns True if '!done' sentence was received in GetOne
Shifts to the next sentence received in GetAll
Cancels current command execution.
Sample APITest application can be downloaded in Releases section: https://github.com/Chupaka/Delphi-RouterOS-API/releases
At first, we should declare a variable and create an instance of TRosApiClient:
var
RouterOS: TRosApiClient;
RouterOS := TRosApiClient.Create;Now we connect to the router and perform login procedure:
if RouterOS.Connect('192.168.0.1', 'admin', 'password') then
begin
//we are connected successfully
end
else
begin
//an error occured; text error message is in LastError property
end;All queries are done by calling Query function of TRosApiClient. It returns an instance of TRosApiResult from which all data is fetched.
var
Res: TRosApiResult;
Res := RouterOS.Query(['/system/resource/print'], True);Res := ROS.Query(['/ip/arp/print', '?interface=ether2'], True);
while not Res.Eof do
begin
SomeProcessingFunction(Res['.id'], Res['address']);
Res.Next;
end;
Res.Free;First, place a Timer on form and name it tmrListen, set Enabled to False. Then we make a query and enable timer:
ResListen := ROS.Query(['/log/listen'], False);
tmrListen.Enabled := True;Then we check for a new data on timer event:
procedure TForm1.tmrListenTimer(Sender: TObject);
begin
repeat
if not ResListen.GetOne(False) then Break;
if ResListen.Trap then
begin
ShowMessage('Trap: ' + ROS.LastError);
Break;
end;
if ResListen.Done then
begin
ShowMessage('Done');
ResListen.Free;
tmrListen.Enabled := False;
Break;
end;
Memo1.Lines.Add(ResListen['time'] + ': ' + ResListen['message']);
until False;
end;For discussions and suggestions you may check MikroTik Forum thread RouterOS API Delphi Client