Community discussions

MikroTik App
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 2:30 pm

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.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 3:01 pm

try
:put [queue simple print as-value where name = test]
p.s. why not API?
Last edited by Chupaka on Thu Jun 25, 2009 3:02 pm, edited 1 time in total.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 3:02 pm

add comment to your rule, then find rule number using comment and remove using that rule number you got.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 3:21 pm

try
:put [queue simple print as-value where name = test]
p.s. why not API?
[admin@MT1] > queue simple print as-value where name=test  
[admin@MT1] > 
Nothing.
Now i look in API side.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 3:27 pm

add comment to your rule, then find rule number using comment and remove using that rule number you got.
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/
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 3:58 pm

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.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:01 pm

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;
}
?>
Last edited by SpectatorCN on Thu Jun 25, 2009 4:47 pm, edited 3 times in total.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:03 pm

SpectatorCN: can you see the difference?
:put [queue simple print as-value where name = test]
queue simple print as-value where name=test 
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:05 pm

Features:
1) Remove the color of characters in the console
sorry for late advice... use
$mtlogin = "user+ct";
instead of
$mtlogin = "user";
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:16 pm

Features:
1) Remove the color of characters in the console
sorry for late advice... use
$mtlogin = "user+ct";
instead of
$mtlogin = "user";
Where were You before?
Thank you.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:23 pm

SpectatorCN: can you see the difference?
I do not like when my nickname provide bold, but still thanks.
Just drew attention to the put.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Thu Jun 25, 2009 4:46 pm

Thanks to all.
Everything works.
I edited the script above.
All excellent.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 8:46 am

i would also ask - wouldn't it be easer to use API to execute the commands instead of emulating telnet client.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 2:45 pm

Sometimes not.
Telnet simple.
And in API no 'find' & no 'where' to select 1 element, rather than all at once.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 3:11 pm

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
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 4:52 pm

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
So your posts should not believe?
http://forum.mikrotik.com/viewtopic.php ... ue#p130214
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.
 
User avatar
SpectatorCN
newbie
Topic Author
Posts: 34
Joined: Tue Sep 23, 2008 9:40 pm
Location: Ukraine
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 5:11 pm

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.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 5:19 pm

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

9 monthes - and 'Queries' in API were born :D
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: Remote Control of traffic shaping over SSH, Telnet

Fri Jun 26, 2009 5:21 pm

how to send multiple parameters.
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]