Community discussions

MikroTik App
 
ninet
just joined
Topic Author
Posts: 14
Joined: Mon Aug 10, 2009 3:58 pm

Php SSH2 fetch OUTPUT from Mikrotik

Wed Aug 12, 2009 11:16 pm

Hello,

I have started with PHP API (topic http://forum.mikrotik.com/viewtopic.php?f=9&t=34084), but wanted to keep same CLI syntax, what is not possible what I didn`t know at the beginning. Since I wanted to preserve original syntax, I have found SSH2 for php, and despite tricky installation, I was delighted how things going on. However, after executing any command without issue, naturally, now I wanna fetch output, but there is the problem. Here is the code:
            // collect returning data from command original
            stream_set_blocking( $stream, true );
            $data = "";
            while( $buf = fread($stream,4096) ){
                $data .= $buf;
            }
            fclose($stream);
I have tried withoud while loop, but still nothing. Accidentally, I got the output from only this command:
/ip firewall filter print
presume because it`s larger amount of data or maybe it took a bit more time to fetch result...don`t know, I have tried to "slow down" with sleep(1), but still nothing, any help would be really appreciated since this part is missing to start connect my php/mysql billing database with mikrotik network.

Regards,
S.
 
fosben
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Thu Dec 14, 2006 4:50 pm

Re: Php SSH2 fetch OUTPUT from Mikrotik

Thu Aug 13, 2009 10:38 am

 
PortalNET
Member Candidate
Member Candidate
Posts: 126
Joined: Sun Apr 02, 2017 7:24 pm

Re: Php SSH2 fetch OUTPUT from Mikrotik

Wed Sep 22, 2021 4:31 pm

ok nearly 12 years later... but as we know.. the PHP api stopped working on the latest versions of Mikrotik 6.4xx so why not use ssh2_connect in order to send cmds and fetch the output result from mikrotik

1st you need to have a server machine the first cmd will depend on your linux distro version, i am using linux-debian distros so this is the cmd in order to install the correct libraries

apt-get install php-ssh2

once installed you can start working on your connections

you need to create a connection script example: connect-mkt.php and add the code below
<?php

       $methods = array('kex' => 'diffie-hellman-group1-sha1');

       $connection = ssh2_connect('IP-OF-YOUR-MIKROTIK', PORT, $methods);
       if ($connection) {
       //echo "connection established\n\n";
       if (ssh2_auth_password($connection, 'mikrotik-username', 'mikrotik-password')) {
       sleep(2);
	   fclose($s);
        }
        else {
        echo "connection failed, please check your login credentials\n";
        }
      
	   }
?>


2nd you will need to create your PHP page flavour with your cmds exec and readout function you can use test php page like the one below

[quote]
<?php 
        include_once("connect-mkt.php");
      
		$s = ssh2_exec($connection, '/system health gauges print');
		stream_set_blocking($s,true);
		$stream_out = ssh2_fetch_stream($s, SSH2_STREAM_STDIO);
	    //echo stream_get_contents($stream_out);
		$output1 = stream_get_contents($stream_out);
		echo "<pre>{$output1}</pre>";
        sleep(2);
		$s = ssh2_exec($connection, '/system identity print');
		stream_set_blocking($s,true);
		$stream_out = ssh2_fetch_stream($s, SSH2_STREAM_STDIO);
	    //echo stream_get_contents($stream_out);
		$output2 = stream_get_contents($stream_out);
		echo "<pre>{$output2}</pre>";
        sleep(2);
		$s = ssh2_exec($connection, '/system clock print');
		stream_set_blocking($s,true);
		$stream_out = ssh2_fetch_stream($s, SSH2_STREAM_STDIO);
	    //echo stream_get_contents($stream_out);
		$output3 = stream_get_contents($stream_out);
		echo "<pre>{$output3}</pre>";
        sleep(2);
		$s = ssh2_exec($connection, '/system routerboard print');
		stream_set_blocking($s,true);
		$stream_out = ssh2_fetch_stream($s, SSH2_STREAM_STDIO);
	    //echo stream_get_contents($stream_out);
		$output4 = stream_get_contents($stream_out);
		echo "<pre>{$output4}</pre>";
        sleep(2);
		fclose($s);
        
        
        ?>
[/quote]


3rd if you wish to output the result on a html form table or row.. you just need to use the code below inside your html form or table

[quote]
value="<?php echo "{$output1}";?>
value="<?php echo "{$output2}";?>
value="<?php echo "{$output3}";?>
value="<?php echo "{$output4}";?>
[/quote]

Who is online

Users browsing this forum: Ahrefs [Bot] and 18 guests