fix routeros api support (#862)

* Add files via upload

support api-ssl for post MikroTik 6.45.1

* Update README.md

* Update README.md
This commit is contained in:
Max Dobladez 2020-10-12 14:59:35 -03:00 committed by GitHub
parent c87cd905c6
commit 1623dcc0a3
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 18 deletions

View File

@ -51,6 +51,8 @@ sudo fcli commit
Changelog
---------
v1.1 - 12 Oct 2020 - fix RouterOS API-ssl to support post MikroTik 6.45.1
v1.0 - 4 Jul 16 - Initial version
Author: Maximiliano Dobladez info@mkesolutions.net

View File

@ -1,9 +1,13 @@
<?php
/*****************************
*
* RouterOS PHP API class v1.6
* * * Actualizado a la version 6.45.1
* * Nuevo api-ssl
*
* RouterOS PHP API class v1.6.1
* Author: Denis Basta
* Contributors:
* Maxi Dobladez <elmaxi[at]gmail[dot]com>
* Nick Barnes
* Ben Menking (ben [at] infotechsc [dot] com)
* Jeremy Jefferson (http://jeremyj.com)
@ -21,9 +25,10 @@ class RouterosAPI
var $connected = false; // Connection state
var $port = 8728; // Port to connect to (default 8729 for ssl)
var $ssl = false; // Connect using SSL (must enable api-ssl in IP/Services)
var $timeout = 2; // Connection attempt timeout and data read timeout
var $attempts = 2; // Connection attempt count
var $timeout = 3; // Connection attempt timeout and data read timeout
var $attempts = 1; // Connection attempt count
var $delay = 2; // Delay between connection attempts in seconds
var $socket; // Variable for storing socket resource
var $error_no; // Variable for storing connection error number, if any
var $error_str; // Variable for storing connection error text, if any
@ -91,28 +96,40 @@ class RouterosAPI
*
* @return boolean If we are connected or not
*/
public function connect($ip, $login, $password)
public function connect($ip, $login, $password,$port=8728)
{
for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
$this->connected = false;
$PROTOCOL = ($this->ssl ? 'ssl://' : '' );
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
$this->socket = @fsockopen($PROTOCOL . $ip, $this->port, $this->error_no, $this->error_str, $this->timeout);
$context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $port . '...');
$this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
if ($this->socket) {
socket_set_timeout($this->socket, $this->timeout);
$this->write('/login');
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=password=' . $password);
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if ($RESPONSE[0] == '!done') {
$this->connected = true;
break;
if (isset($RESPONSE[0])) {
if ($RESPONSE[0] == '!done') {
if (!isset($RESPONSE[1])) {
// Login method post-v6.43
$this->connected = true;
break;
} else {
// Login method pre-v6.43
$MATCHES = array();
if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
$RESPONSE = $this->read(false);
if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
$this->connected = true;
break;
}
}
}
}
}