Random failure in connection with mikrotik using the PHP API

Hi community friends,

I try to connect mikrotik with script below and i get success but in random situation: Sometimes connects, sometimes fails to connect.

Has anyone gone through this experience (I’m using the PHP API)?

The code:

<?php
function gravarequipamento() 
{
	if($this->data['Equipamento']['form'] == 'gravarequipamento')
	{
		$this->data = Sanitize::clean($this->data, array('encode' => false));
		
		$this->Equipamento->set($this->data);
		
		if ($this->Equipamento->validates()) 
		{   
			// DEFINIÇÕES DO SISTEMA
			define('USER_PADRAO', 'manut'); // System User
			define('GRUPO_PADRAO', 'write'); // Group System User
			define('ADMIN_SISTEMA', 'zuca'); // System Admin test
			define('SENHA_SISTEMA', '123456'); // Password test
			$ip = $this->data['Equipamento']['ip']; // IP of MIKROTIK
			
			
			$new_password = // Encryption omitted; 
			
			$this->data['Equipamento']['senha'] = $new_password;

			// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ACTION IN MIKROTIK
			require('routeros_api.class.php'); 
			$API = new routeros_api();
			$API->debug = false; 
			
			if ($API->connect($ip, ADMIN_SISTEMA, SENHA_SISTEMA))  // ============> Here failure occurs randomly
			{
					$this->Equipamento->save($this->data); // Save in data together with mikrotik action. If one fail, another fail to.
				
					$API->write('/user/add', false);
					$API->write('=group='.GRUPO_PADRAO, false);
					$API->write('=name='.USER_PADRAO, false);
					$API->write('=password='.$new_password);
					
					$ARRAY = $API->read();
					$API->disconnect();
					$mensagem = 'SAVE SUCESS!
					<br/> USUÁRIO PADRÃO: '.USER_PADRAO.'
					<br/>SENHA: '.$new_password;
					$this->Session->setFlash($mensagem);
					$this->redirect(array('action' => 'gravarequipamento'));
			}
			else 
			{
					$mensagem = 'FAIL CONECTION WITH MIKROTIK ('.$ip.')';
					$this->Session->setFlash($mensagem);
					$this->redirect(array('action' => 'gravarequipamento'));
			}
			// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ACTION IN MIKROTIK
		} 
		else 
		{
			// Tratando as mensagens de erro
			$mensagem = $this->Equipamento->invalidFields();
			if(isset($mensagem['ip'])){$ip = ">> ".$mensagem['ip']." <<";} else {$ip = '';}
			if(isset($mensagem['nome'])){$nome = ">> ".$mensagem['nome']." <<";} else {$nome = '';}
			$this->Session->setFlash("Please, read the instructions here:<br/>".$ip."<br/>".$nome);

			//if(!empty($mensagem)){$this->Session->setFlash($mensagem);}
			$this->redirect(array('action' => 'gravarequipamento'));
		}

		
		
	} 
} 
?>

Thanks any help!

fails with what error?..

Hi Chupaka,

Glad to see you again.

When i try to conect to execute add or remov comand with API PHP i receive this:


Connection attempt #1 to xxx.xxx.xxx.xxx:8728...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 0] !trap
Connection attempt #2 to xxx.xxx.xxx.xxx:8728...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 0] !trap
Connection attempt #3 to xxx.xxx.xxx.xxx:8728...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 0] !trap
Connection attempt #4 to xxx.xxx.xxx.xxx:8728...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 0] !trap
Connection attempt #5 to xxx.xxx.xxx.xxx:8728...
<<< [6] /login
>>> [5/5 bytes read.
>>> [5, 0] !trap
Error...

In localhost is all right, without fail. When i put it in server on line, i have a random failure conection. I don´t understend what happening.

My code to remove:

function apagarequipamento($id) 
{
		$dadosplaca = $this->Equipamento->findById($id);
		
		// DEFINIÇÕES DO SISTEMA
		define('USER_PADRAO', 'manut'); // User 
		define('ADMIN_SISTEMA', 'zuca'); // Admin system
		define('SENHA_SISTEMA', '123456'); // test password
		$ip = $dadosplaca['Equipamento']['ip']; // IP of MIKROTIK
		
		
		// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ACTION IN MIKROTIK
		require('routeros_api.class.php'); // API PHP 
		$API = new routeros_api();
		$API->debug = true; 
		
		if ($API->connect($ip, ADMIN_SISTEMA, SENHA_SISTEMA)) // ****** Problem, i think, is here. But why on line and localhost not?
		{
				$API->write('/user/remove', false);
				$API->write('=.id='.USER_PADRAO);
				
				$ARRAY = $API->read();
				
				$this->Equipamento->delete($id); // Save data. If mikrotik fail, this fail to.
				
				$API->disconnect();
				$mensagem = 'REMOVED WITH SUCESS!
				<br/> PLACA: '.$ip;
				$this->Session->setFlash($mensagem);
				$this->redirect(array('action' => 'index'));
		}
		else 
		{
				$mensagem = 'FAILURE ('.$ip.').  // [i][u]I receive this message when debug is false[/u][/i]
				<br/> >> USER NOT REMOVE << 
				<br/> >> DAT NOT REMOVE <<';
				$this->Session->setFlash($mensagem);
				$this->redirect(array('action' => 'index'));
		}
		// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: AÇÃO NO MIKROTIK

	}

}

maybe you can sniff that communication when error occurs? also, what version of ROS do you use?

I’m using the PHP API and Winbox to support the tests.

What command should I use in terminal of winbox to activate the sniff? I´m looking for it in the terminal but i´m newbe… :confused:

I see in window bar of winbox v4.5 RB433. I think is the version, no?

use Wireshark or something

yep. at first, try to upgrade to v4.11 and see whether your problem is still here

Maybe is the problem, no?

http://forum.mikrotik.com/t/php-api-bug/25484/4

It was fixed when I followed the suggestion found here:

http://forum.mikrotik.com/t/api-login-problem-on-rb133/27084/3

I can not tell if this is the problem, but it worked.

The sleep before read() in connect() fixed the problem at the moment. For me, 2 seconds its cool. :astonished: