Remote Control of traffic shaping over SSH, Telnet

I am writing script for remote control MT via telnet or ssh, and faced with the problem

A simple example:

Add rule:
“queue simple add name = test target-address = 10.10.10.2/32 max-limit = 512000/512000”
Rule added at number 3016
All excellent.

But the rule is necessary to remove by it number.
To delete a need to write "queue simple remove 3016
How to find the rules through telnet?

If do “queue simple print brief” this is a very long time and it is necessary to transmit the symbol “Q” to continue.

If do “queue simple print brief where name = test” that will give

Flags: X - disabled, I - invalid, D - dynamic 
# TARGET-ADDRESSES DST-ADDRESS INTER ... LIMIT-AT MAX-LIMIT BURST-LIMIT 
0 10.10.10.2/32 0.0.0.0 / 0 all 0 / 0 512k/512k 0 / 0

where the number of rule = 0, but this is not true.

try

:put [queue simple print as-value where name = test]

p.s. why not API?

add comment to your rule, then find rule number using comment and remove using that rule number you got.



[admin@MT1] > queue simple print as-value where name=test  
[admin@MT1] >

Nothing.
Now i look in API side.

I am search by name. And can’t find number.
How find rule number using comment?
queue simple print where comment=“test” ???

Don’t work. It show wrong number/

it shows ID and you have to use ID, it is not a simple number as in RouterOS 2.9.xx that is the difference

also if you look at API side, you get the same ID numbers not some obscure numbering number that will change nobody knows when.

This is my simple script written in PHP.
It may be useful to someone.
Features: reading all from the port after send command

#!/usr/bin/php
<?php
$mthost = "10.0.0.1";
$mtlogin = "user";
$mtpass = "password";


# Заголовок для telnet
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x41).chr(0x4E).
chr(0x53).chr(0x49).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);


# коннект
$fp=fsockopen($mthost,23);

# Посылаем заголоки
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);

# авторизируемся
write_to_telnet($fp,$mtlogin."+ct");
usleep(125000);
write_to_telnet($fp,$mtpass);
usleep(125000);

$rez = read_from_telnet($fp);

write_to_telnet($fp,":put [queue simple print as-value where name=test]");
$rez = read_from_telnet($fp);
echo $rez;

write_to_telnet($fp,"quit");
fclose($fp);
echo "\n";
exit();

function write_to_telnet($fp, $text){ 
         fputs($fp,$text."\r\n");
return true;
}
function read_from_telnet($fp){
    $output = "";
    $count = 0;
    do{
        $char =fread($fp, 1);
        $output .= $char;
        if($char==">") $count++;
        if($count==1) break;
    }while(1==1);
return $output;
}
?>

SpectatorCN: can you see the difference?



sorry for late advice… use

$mtlogin = "user+ct";

instead of

$mtlogin = "user";

Where were You before?
Thank you.

I do not like when my nickname provide bold, but still thanks.
Just drew attention to the put.

Thanks to all.
Everything works.
I edited the script above.
All excellent.

i would also ask - wouldn’t it be easer to use API to execute the commands instead of emulating telnet client.

Sometimes not.
Telnet simple.
And in API no ‘find’ & no ‘where’ to select 1 element, rather than all at once.

omg, I’m again too late =) http://wiki.mikrotik.com/wiki/API#Queries

and telnet is simplier for human. API is for programs, it’s more definite

So your posts should not believe?
http://forum.mikrotik.com/t/remove-active-user-hotspot-php-api/23834/2

and telnet is simplier for human. API is for programs, it's more definite

I agree, if they are equal in functionality.
In my opinion the program is still telnet or API. Main as I prefer.

To be honest API for PHP in the wiki describes how disgusting.
And the search showed that no one I could not understand how to send multiple parameters.

$API->write('/queue/simple/print',false);
$API->write('?name='.$name);
$ARRAY = $API->read();

Really works.
This is a good thing.

haha, you’re so funny )) look at the date of that post - 25 Sep 2008 =)

9 monthes - and ‘Queries’ in API were born :smiley:

like this:

$API->write('/my/command/here',false);
$API->write('=param1='.$param1,false);
$API->write('=param2='.$param2,false);
$API->write('?query1='.$query1,false);
$ARRAY = $API->read();

[/quote]