Community discussions

MikroTik App
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

slow ssh commands executing from remote script

Mon Aug 15, 2011 7:57 am

I'm writing a simple script on php, that adding and removing entries to/from mikrotik's address-list. I'm using phpseclib to make ssh connections.

It works, but slowly - about 1 second for each command passed to mikrotik.

Here's detailed info on latencies.
From begining to login - 0.10053205490112 sec
1st command - 1.006432056427 sec (/ip firewall address-list remove [/ip firewall address-list find comment="{$guid}"])
2nd command - 1.0368900299072 sec (/ip firewall address-list add list="{$group}" address="{$ip}" comment="{$guid}")
From disconnecting to end - 0.00031399726867676 sec

Is it mikrotik or phpseclib problem? Is there other solutions?
Sadly, but php_ssh extension not working on windows and crashing apache.

PS: RB 1100, ROS 5.5. Windows 2003 on billing server.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 8:01 am

Is there any specific reason that you're not using the API?
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 8:52 am

Is there any specific reason that you're not using the API?
Which API?

Billing, that I want to communicate with mikrotik, have a nice feature, when user change his state (authorized/unauthorized) - it will execute a script (if specified). So when user authorizes or unauthorizes - billing executing script, which send command to mikrotik (billing and mikrotik are separated on different hardware), allowing or disallowing internet to user. So all I need, is to find fast and good way to communicate billing with mikrotik.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 9:00 am

Look at this: http://wiki.mikrotik.com/wiki/Manual:API

There are PHP libraries on the wiki.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 10:33 am

Look at this: http://wiki.mikrotik.com/wiki/Manual:API

There are PHP libraries on the wiki.
Wow, just wow. :shock:
That's very fast, 30ms vs 1000ms. Thank you!
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 12:01 pm

5.6 should also be faster to execute commands through SSH, but APi will beat that anyway.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 12:44 pm

Sadly, but 30ms was when I used incorrect commands...
When I made script working - it's slow again...
$stt = microtime(true);
$API = new routeros_api();
$API->debug = false;

if($API->connect($cfg["mt_host"], $cfg["mt_username"], $cfg["mt_password"]))
{
	// Id finding
	$id = $API->comm("/ip/firewall/address-list/print", array(
		".proplist" => ".id",
		"?comment" => "{$guid}",
		));
			
	// Deleting address by id
	$API->comm("/ip/firewall/address-list/remove", array(
		".id" => $id[0][".id"],
		));
			
	// Adding address
	$API->comm("/ip/firewall/address-list/add", array(
		"list" => "{$group}",
		"address" => "{$ip}",
		"comment" => "{$guid}",
		));
			
	$API->disconnect();
			
	$tm = microtime(true) - $stt;
	if(IN_DEBUG) file_put_contents("C:\\log.txt", "( " . $tm . " )\r\n", FILE_APPEND);
}
1.0343606948853 sec :(((


Is there a way to make it faster?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 3:10 pm

print with limitation is taking the time, and you can check how many IP addresses are in the list. Problem, it seems is that you have a lot of entries and just print takes the time you are measuring.

Measure just how long initial print takes. Remove by ID should be fast.

And how many entries are in the list you are searching.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Aug 15, 2011 5:09 pm

And how many entries are in the list you are searching.
6 entries
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Fri Aug 19, 2011 11:56 am

Can I define .id value manually to speed up list searching?
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: slow ssh commands executing from remote script

Fri Aug 19, 2011 12:31 pm

as for me, adding of 41 entries takes 9 sec, and printing of the whole list of ~1k entries takes <1 sec

so, check time of each operation separately - seems like /print is not as long as modifying...
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Fri Aug 19, 2011 1:21 pm

I checked the time, that every command consumes. Is it normal?
$API->connect($cfg["mt_host"], $cfg["mt_username"], $cfg["mt_password"]); // 0.34563493728638 sec
$id = $API->comm("/ip/firewall/address-list/print", array(".proplist" => ".id", "?comment" => "{$guid}")); // 0.21873903274536 sec
$API->comm("/ip/firewall/address-list/remove", array(".id" => $id[0][".id"])); // 0.21874403953552 sec
$API->comm("/ip/firewall/address-list/add", array("list" => "{$group}","address" => "{$ip}","comment" => "{$guid}")); // 0.21876692771912 sec

$API->connect($cfg["mt_host"], $cfg["mt_username"], $cfg["mt_password"]); // 0.35410308837891 sec
$id = $API->comm("/ip/firewall/address-list/print", array(".proplist" => ".id","?comment" => "{$guid}")); // 0.10922288894653 sec
$API->comm("/ip/firewall/address-list/remove", array(".id" => $id[0][".id"],)); // 0.21878099441528 sec
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: slow ssh commands executing from remote script

Fri Aug 19, 2011 2:01 pm

so, you cannot speed up the things - printing consumes even less resources than removing by .id
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Fri Aug 19, 2011 4:33 pm

there is something terribly wrong with php implementation then. :)

I have written my C++ (along with Java)

and there
[admin@RB1000] /ip firewall address-list> print count-only 
13
6 addititions and along with 6 removes
does this in less than 1 second:

$ time ./TestAPI rb1000 list.tst 
Attempting connect to bs
Connected to bs
Going to read file list.tst 
list.tst size is: 12
test command dump start:
________________________
Command out:
        |/ip/firewall/address-list/add
        |=address=1.1.1.1
        |=list=tst
Command out:
        |/ip/firewall/address-list/remove
        |=.id=<var>
Command out:
        |/ip/firewall/address-list/add
        |=address=1.1.1.1
        |=list=tst
Command out:
        |/ip/firewall/address-list/remove
        |=.id=<var>
Command out:
        |/ip/firewall/address-list/add
        |=address=1.1.1.1
        |=list=tst
Command out:
        |/ip/firewall/address-list/remove
        |=.id=<var>
   mand out:                                                                                                                                 |/ip/firewall/address-list/add



        |/ip/firewall/address-list/remove
        |=.id=<var>
Command out:
        |/ip/firewall/address-list/add
        |=address=1.1.1.1
        |=list=tst
Command out:
        |/ip/firewall/address-list/remove
        |=.id=<var>
Command out:
        |/ip/firewall/address-list/add
        |=address=1.1.1.1
        |=list=tst
Command out:
        |/ip/firewall/address-list/remove
        |=.id=<var>

________________________
test command dump end.
DEBUG: getVal 0
DEBUG: getVal 1
DEBUG: getVal 0
DEBUG: getVal 1
DEBUG: getVal 0
DEBUG: getVal 1
DEBUG: getVal 0
DEBUG: getVal 1
DEBUG: getVal 0
DEBUG: getVal 1
DEBUG: getVal 0
All list.tst tests passed

real    0m0.521s
user    0m0.000s
sys     0m0.000s
that includes -> reading from file, parse the commands, execute the comands and update value of <var> after each execute.

however it is RB1000 and it is idling for most time of the day.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Thu Aug 25, 2011 12:45 am

there is something terribly wrong with php implementation then. :)
I tried other implementations and seems you're right.

Sadly C++ implementation is only for linux, even after rewriting it for winsock2 - it crashing with assertion error about string subscript out of range, if not that, it will be the fastest one...

C# API implementations works only when there's a need in sending commands, it goes in infinite loop on ".id" reading after proper query, seems to be a bug in an implementation code. I've succesfully added new entry in address-list. It took about 500 msec. It's a good progress.

I'll try the C implementation tomorrow.

Any help will be appreciated.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Sun Aug 28, 2011 4:42 am

Well... after few hours of rewriting (to make it use winsock2 sockets) and then testing C implementation. I have not a bad results:

1 login
1 to 2 commands
1 logout

...took on average 650 msec. It's roughly ~2 times faster than php one and ~4 times faster than php through ssh implementation.

If anyone wants, I can post working mikrotik-api.c rewritten for winsock2.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Mon Aug 29, 2011 9:33 am

you can post it on wiki (have to register on wiki.mikrotik.com) - just make article and place link here i will add to pages that are not editable for other users.

SSH through php is something mindbending - you are using php to create and maintain ssh connection and there the console runs. No surprise that it is slow as you are running layer upon layer there.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Aug 29, 2011 12:21 pm

SSH through php is something mindbending - you are using php to create and maintain ssh connection and there the console runs. No surprise that it is slow as you are running layer upon layer there.
phpseclib have its own SSH client written on pure PHP. I thought mikrotik does all operations in something like 100-200 msec (including login, command execution, logout), and it's not a problem to use PHP at all. It was a bad idea...

I'll post sources on wiki tomorrow. Should I post all sources, or just one changed file - mikrotik-api.c ?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Mon Aug 29, 2011 3:04 pm

it takes some time to parse the command for CLI connection, and some time to execute it. That comes with overhead. PHP after all is script language. maybe some PHP booster could help in this case with PHP code.

Maybe converting PHP to C++ and compiling it for your system would also increase the speed.

You can set code in wiki up as you want, you can reference unchanged code, or just place it all together in one page.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Tue Aug 30, 2011 9:54 pm

you can post it on wiki (have to register on wiki.mikrotik.com) - just make article and place link here i will add to pages that are not editable for other users.
http://wiki.mikrotik.com/wiki/API_in_C_using_winsock

Maybe I'm not good at explaining things in the beginning of the page, please correct me.
English isn't my native language, I'm russian.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Wed Aug 31, 2011 4:27 pm

thank you for your contribution :)
 
daphne22
just joined
Posts: 1
Joined: Mon Sep 05, 2011 2:10 pm

Re: slow ssh commands executing from remote script

Mon Sep 05, 2011 2:18 pm

SSH through php is something mindbending - you are using php to create and maintain ssh connection and there the console runs. No surprise that it is slow as you are running layer upon layer there.
phpseclib have its own SSH client written on pure PHP. I thought mikrotik does all operations in something like 100-200 msec (including login, command execution, logout), and it's not a problem to use PHP at all. It was a bad idea...

I'll post sources on wiki tomorrow. Should I post all sources, or just one changed file - mikrotik-api.c ?
PHP API is slow too as OP noted. You can benchmark stuff with phpseclib. See where the bottlenecks are. Bottleneck is probably in mikrotik tbh gven PHP API performance.

You could also try it with PuTTY. Do remote command in SSH section. If it's faster in the SSH console (as opposed to remote command section) try read() and write() with phpseclib and not exec().
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Tue Sep 06, 2011 1:36 pm

If it's faster in the SSH console (as opposed to remote command section) try read() and write() with phpseclib and not exec().
In SSH console it's does fast. 4-6 seconds for 100 entries.
I tried write() commands. It's just didn't wait for command to complete. For that readon I used read() to avoid that. write + read completed in the exactly same time as exec.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: slow ssh commands executing from remote script

Tue Sep 06, 2011 4:30 pm

there are several things to consider:
* what router you are using?
* php is scripting language, they are slow (slower than C/C++


in my experience API is the fastest and safest method to communicate with the router from application.
 
hel
Member Candidate
Member Candidate
Topic Author
Posts: 199
Joined: Sun Jun 12, 2011 6:31 am
Location: Kirov, Russia

Re: slow ssh commands executing from remote script

Mon Sep 19, 2011 10:28 am

Well... After investigation on that problem, I've found that it was because of specific windows 2003 server configuration and it's not a mikrotik problem at all.

Server had Routing and Remote Access + NAT and Traffic Inspector in router mode enabled.
Average script execution time was 800 msec.

After removing Routing and Remote Access service + NAT, and reconfiguring Traffic Inspector in "sniffing" mode, very first seconds on the RDP I've noticed that all lags I've experienced before is now disappeared.
Now average script execution time is 100 msec.

Who is online

Users browsing this forum: Bing [Bot], dervomsee, jaclaz, maromo, rextended and 129 guests