Sorry I can’t help you with the PHP API library. But for anyone using the Ruby API library, it looks like:
require irb(main):001:0> require 'mtik'
=> true
irb(main):002:0> con = MTik::Connection.new(:host=>'10.0.0.1',:user=>'user',:pass=>'password')
=> #<MTik::Connection:0x000001009d2c80 @sock=#<TCPSocket:fd 5>, @requests={}, @host="10.0.0.1", @port=8728, @user="user", @pass="password", @conn_timeout=60, @cmd_timeout=60, @data="", @parsing=false>
irb(main):003:0> r = con.get_reply('/ping','=address=10.0.0.2','=count=3')
=> [{"!re"=>nil, "host"=>"10.0.0.2", "size"=>"56", "ttl"=>"255", "time"=>"00:00:00.006", "sent"=>"1", "received"=>"1", "packet-loss"=>"0", "min-rtt"=>"00:00:00.006", "avg-rtt"=>"00:00:00.006", "max-rtt"=>"00:00:00.006", ".tag"=>"6"}, {"!re"=>nil, "host"=>"10.0.0.2", "size"=>"56", "ttl"=>"255", "time"=>"00:00:00.003", "sent"=>"2", "received"=>"2", "packet-loss"=>"0", "min-rtt"=>"00:00:00.003", "avg-rtt"=>"00:00:00.004", "max-rtt"=>"00:00:00.006", ".tag"=>"6"}, {"!re"=>nil, "host"=>"10.0.0.1", "size"=>"56", "ttl"=>"255", "time"=>"00:00:00.003", "sent"=>"3", "received"=>"3", "packet-loss"=>"0", "min-rtt"=>"00:00:00.003", "avg-rtt"=>"00:00:00.004", "max-rtt"=>"00:00:00.006", ".tag"=>"6"}, {"!done"=>nil, ".tag"=>"6"}]
irb(main):004:0> puts "MINIMUM: #{r[-2]['min-rtt']}\tMAXIMUM #{r[-2]['max-rtt']}\tAVERAGE: #{r[-2]['avg-rtt']}"
MINIMUM: 00:00:00.003 MAXIMUM 00:00:00.006 AVERAGE: 00:00:00.004
=> nil
Or a Ruby script that takes 4 or 5 arguments, the MikroTik device IP to execute the API command, the username to authenticate as, the password, the IP to ping, and an optional integer ping packet count:
#!/usr/bin/env ruby
require 'mtik'
con = MTik::Connection.new(:host => ARGV[0], :user => ARGV[1], :pass => ARGV[2])
count = ARGV[4] || '5'
count = count.to_i
result = con.get_reply('/ping', "=address=#{ARGV[3]}", "=count=#{count}")
sent = result[-2]['sent'].to_i
received = result[-2]['received'].to_i
loss = 100.0 * (sent - received) / sent.to_f
min = result[-2]['min-rtt'] || '-'
max = result[-2]['max-rtt'] || '-'
avg = result[-2]['avg-rtt'] || '-'
puts "PING FROM #{ARGV[0]} => #{ARGV[3]} (#{count} packets):"
puts "SENT: #{sent}\tRECEIVED: #{received}\tPACKET LOSS: #{loss}%\tMINIMUM: #{min}\tMAXIMUM #{max}\tAVERAGE: #{avg}"
If the above script were named pingscript.rb in the current working directory, one might use it from a 'nix shell like this:
user@host:~$ ./pingscript.rb 10.0.0.1 'user' 'password' 10.0.0.2
PING FROM 10.0.0.1 => 10.0.0.2 (5 packets):
SENT: 5 RECEIVED: 5 PACKET LOSS: 0.0% MINIMUM: 00:00:00.002 MAXIMUM 00:00:00.005 AVERAGE: 00:00:00.003
user@host:~$ ./pingscript.rb 10.0.0.1 'user' 'password' 10.0.0.2 20
PING FROM 10.0.0.1 => 10.0.0.2 (20 packets):
SENT: 20 RECEIVED: 20 PACKET LOSS: 0.0% MINIMUM: 00:00:00.002 MAXIMUM 00:00:00.006 AVERAGE: 00:00:00.004
user@host:~$