Community discussions

MUM Europe 2020
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

[SOLVED] Ping Via API

Wed Sep 28, 2011 5:09 pm

Hi.. i'm trying to do a ping via API, but i'm not getting to ping more than one time...

     $API->write('/ping',false);
     $API->write("=address=$NASIPAddress"); 
     $API->write('=count=3');
     $API->write('=interval=1');
     $API->write('=ret=');
     $ARRAY4 = $API->read(false);
    print_r($ARRAY4);

I put =count=3 , because I need the avg from the ping...

but look the return of command
Array ( 
 [0] => !trap 
 [1] => =message=no such command prefix 
 [2] => !done
 [3] => !trap 
 [4] => =message=no such command prefix 
 [5] => !done 
 [6] => !trap 
 [7] => =message=no such command prefix 
 [8] => !done 
 [9] => !re 
 [10] => =host=192.168.2.5
 [11] => =size=56 
 [12] => =ttl=64 
 [13] => =time=00:00:00.001 
[b][color=#FF0000] [14] => =sent=1 [/color][/b]  (I put 3 on count... and its sending one..)
 [15] => =received=1
 [16] => =packet-loss=0 
 [17] => =min-rtt=00:00:00.001 
 [18] => =avg-rtt=00:00:00.001 
 [19] => =max-rtt=00:00:00.001 
)
Last edited by alissonx on Mon Oct 10, 2011 3:17 pm, edited 2 times in total.
 
User avatar
astounding
Member Candidate
Member Candidate
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Re: Ping Via API

Thu Sep 29, 2011 12:06 am

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:~$
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Sep 29, 2011 12:38 am

in your code have ""sent"=>"3""


in my I put 3 on count... but just sent one...

and I need to use src-address...

but.. don't work...

I need to do

/ping count=3 src-address=192.168.1.1 address=192.168.1.2

via api...
 
User avatar
maximan
Trainer
Trainer
Posts: 549
Joined: Sat May 29, 2004 12:10 am
Location: Rio Cuarto, Argentina
Contact:

Re: Ping Via API

Thu Sep 29, 2011 2:12 am

Try This:
...init and conect ....
$API->write('/ping',false);
$API->write('=address=8.8.8.8',false); 
$API->write('=count=3',false);
$API->write('=interval=1');
$ARRAY4 = $API->read(false);
echo "<pre>". print_r($ARRAY4) ." </pre>";
$API->disconnect();
M.
MKE Solutions > Professional Support IT (Spanish / English)
FastNetMon / FNM Manager: DDoS Detection Tools.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Sep 29, 2011 3:18 pm

and how to use the src-addres?

ping src-address=192.168.1.1 address=192.168.1.2
 
User avatar
maximan
Trainer
Trainer
Posts: 549
Joined: Sat May 29, 2004 12:10 am
Location: Rio Cuarto, Argentina
Contact:

Re: Ping Via API

Thu Sep 29, 2011 4:24 pm

Just add src-address option
  $API->write('=src-address=192.168.1.1',false);
M.
MKE Solutions > Professional Support IT (Spanish / English)
FastNetMon / FNM Manager: DDoS Detection Tools.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Sep 29, 2011 5:01 pm

I put this code
$API->write('/ping',false);
$API->write('=src-address=192.168.1.1',false);
$API->write('=address=192.168.1.2',false);
$API->write('=count=5',false);
$API->write('=interval=1');
$ARRAY4 = $API->read(false);
echo "<pre>". print_r($ARRAY4) ." </pre>";
but.. doesn't work... stay loading... and don't show anything
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Sep 29, 2011 5:04 pm

if I remove this line... works fine...
$API->write('=src-address=192.168.1.1',false);
 
User avatar
maximan
Trainer
Trainer
Posts: 549
Joined: Sat May 29, 2004 12:10 am
Location: Rio Cuarto, Argentina
Contact:

Re: Ping Via API

Thu Sep 29, 2011 8:40 pm

if it works, give me karma :P

M.
MKE Solutions > Professional Support IT (Spanish / English)
FastNetMon / FNM Manager: DDoS Detection Tools.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Sep 29, 2011 8:48 pm

its working without $API->write('=src-address=192.168.1.1',false);

i need to do with src-address but =\ dont work
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Mon Oct 03, 2011 9:43 pm

My Code
$API->write('/ping',false);
$API->write("=src-address=192.168.1.1");	
$API->write("=address=192.168.1.2");	
$API->write('=count=3');
$API->write('=interval=2');
$API->write('=ret=');
its right? and don't know How to use src-address
 
reverged
Member Candidate
Member Candidate
Posts: 270
Joined: Thu Nov 12, 2009 8:30 am

Re: Ping Via API

Tue Oct 04, 2011 4:09 am

My Code
$API->write('/ping',false);
$API->write("=src-address=192.168.1.1");	
$API->write("=address=192.168.1.2");	
$API->write('=count=3');
$API->write('=interval=2');
$API->write('=ret=');
its right? and don't know How to use src-address
You need a 'false' as the second parameter to each write except the last one.
$API->write('/ping',false);    // send ping command and more is coming
$API->write('=src-address=192.168.1.1',false);   //more after this.
$API->write('=address=192.168.1.2',false);    //more...	
$API->write('=count=3',false);   //more...
$API->write('=interval=2');   // no false as this is the last write for this command.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Tue Oct 04, 2011 3:19 pm

look

without this line everything works fine...

I removed this line >
$API->write('=src-address=192.168.1.1',false);   //more after this.
$API->write('/ping',false);    // send ping command and more is coming
$API->write('=address=192.168.1.2',false);    //more...   
$API->write('=count=3',false);   //more...
$API->write('=interval=2');   // no false as this is the last write for this command.
but I need to put a src-addres because I need to test from server to mikrotik...
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Ping Via API

Tue Oct 04, 2011 3:40 pm

it is not clear for me what direction you want to ping? All you do is send ping from A to B, where A is router and B is your target, that hopefully responds to ICMP packets

And that is that, all you can check ir RTT from A to B and back and where is difference A to B or B to A. It should be the same.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Tue Oct 04, 2011 4:05 pm

can you send me the command to execute on mikrotik terminal?

only with addres e src-address?
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Wed Oct 05, 2011 10:41 pm

can you send me the command to execute on mikrotik terminal?

only with addres e src-address?

my Mikrotik: 192.168.1.1
my server: 192.168.2.1
my customer: 192.168.3.1

I want to ping from the api on mikrotik 192.168.1.1(Routerboard)

the src-address: 192.168.2.1 (my server)
to address:192.168.3.1(customer)

/ping src-address=192.168.2.1 address=192.168.3.1

thats correct?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Ping Via API

Thu Oct 06, 2011 9:30 am

ICMP does not work that way.

ping command needs tha IP address to be on the host you are pinging from. So that host that sends out ICMP packets will receive the replies. Other use of ICMP just do not make sense.

You can go to /tool traffic-generator to make up packet as you wish (with whatever content of packet) and send them that way.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Thu Oct 06, 2011 4:31 pm

look..

I need to ping from server to customer...

so I need to use shell script?

I get it by system("ping $customer")

but its so slow... the ping of mikrotik is more fastest...
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Ping Via API

Fri Oct 07, 2011 8:49 am

if you need to ping from server to customer you go to server and ping from server to customer, you do not go to other device to issue ICMP packets.

Ping tool is binding to IP address you set as src and will fail (as expected) since ip address is not present on the device and should not be present there. Even if ping too could bind to somewhere, somehow and send out these packets with src-address that it does not have, where would these ICMP packets go?

If you want to send random garbage in your network you can do so by using '/tool traffic-generator' where you can set write content of the packet yourself, with MAC addresses and IP address, and IP flags and IP packet contents you like mimicking any packet you like and send them on interface of choice.

edit: If you have 3 hosts in your network A, B and C you cannot run ICMP from A to B from C. You actually have to be on A or B to use ICMP between them. Other way packets from C will reach destination that is either A or B but reply from host will go to original src-address and dropped there as host receiving reply did not issue the ICMP request in the first place.
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: Ping Via API

Fri Oct 07, 2011 3:07 pm

thank you so much...

i've thought that was possible...

:)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: [SOLVED] Ping Via API

Tue Oct 11, 2011 9:04 pm

If you want to programmatically ping from your server (where PHP runs) to your client, you can use something like Net_Ping. The package is not maintained, but that doesn't mean it doesn't work - it simply means the developers aren't working on it, which means that last they checked, it works and they won't be fixing it if it breaks in the future.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
alissonx
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Tue Oct 06, 2009 4:12 pm

Re: [SOLVED] Ping Via API

Tue Oct 11, 2011 9:36 pm

i'm using

$ping="system("ping 192.168.1.1")";

its working... but delay 5 seconds...
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: [SOLVED] Ping Via API

Wed Oct 12, 2011 12:01 am

Looking at the source of Net_Ping, it's basically doing the same thing (using exec()), except that it parses the response... you'd still get a delay of course.

If you have the sockets extension, you can use something like one of the functions provided in the comments of socket_create(). It is sure to be a lot faster. That is assuming you want to go faster ;) .
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: No registered users and 29 guests