Community discussions

MikroTik App
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Enable and Disable Ethernet ports

Thu Oct 03, 2013 12:00 pm

Hi, i'm newbie with API's Mikrotik right now, and I need some help :S

I need know how to enable and disable ethernet ports with api and php..

I was testing with these codes, but i cant fix them..Im so bad :(
if ($API->connect($ipRouteros , $username , $pass, $api_puerto)) {
	{	
	$API->write('/interface/ethernet',false);
	$API->write('=disable=ether4',true);
	}
}
if ($API->connect($ipRouteros , $username , $pass, $api_puerto)) 
	{
	$API->write("interface/ethernet",false);
	$API->write("=SET=");
	$API->write("ether4",false);
	$API->write("=disabled=yes",true);
	
   }    
Thanks :)

P.D: I trying create 2 php, one of them to active an specific ethernet port, and another php to desactive it.
It's my first project with mikrotik API :S
Last edited by Vyrtu on Thu Oct 03, 2013 12:32 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Enable and Disable Ethernet ports

Thu Oct 03, 2013 12:17 pm

Think about how you do it from terminal.

If you do "/interface ethernet ?", you'll see "disable" is a command. If you then do "/interface ethernet disable ?", you'll see it accepts "numbers" as an argument, which is the interface(s) to disable.

Therefore:
$API->write('/interface/ethernet/disable',false);
$API->write('=numbers=ether4',true); 
P.S. I'd really like to know what made you pick that particular PHP client.
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: Enable and Disable Ethernet ports

Thu Oct 03, 2013 12:27 pm

Thanks, but i still having problems. It's my full code:
<?php require_once('routeros_api.class.php'); 
?>

<?php
	

	 $ipRouteros="192.168.2.1"; // tu RouterOS.
     $Username="adminapi"; // usuario API
     $pass="adminapi"; // contraseña del usuario API
     $api_puerto=8728; // Puerto API por defecto
	
$API = new routeros_api();
$API->debug = false;
if ($API->connect($ipRouteros , $username , $pass, $api_puerto)) 
	{
	$API->write('/interface/ethernet/disable',false);
	$API->write('=numbers=ether4',true);
   }    

$API->disconnect();
?>
 
User avatar
Vyrtu
newbie
Topic Author
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Re: Enable and Disable Ethernet ports

Mon Oct 14, 2013 12:59 pm

My problem is solved. I post the final script.
<?php require_once('api_mt_include2.php'); ?>
<?php 
 
////////////////////////////////////////////////////////////////////
// ESTE EJEMPLO SE DESCARGO DE www.tech-nico.com ///////////////////
// Creado por: Nicolas Daitsch. Guatrache. La Pampa ////////////////
// Contacto: administracion@tech-nico.com //////////////////////////
// RouterOS API: Busco interfaz ethernet y la activo o desactivo  //
////////////////////////////////////////////////////////////////////
 
$ipRouteros="200.20.30.40";  // tu RouterOS. 
$Username="blog.tech-nico.com"; 
$Pass="tupassword"; 
$api_puerto=8728; 
$name="ether1";    // -------> aqui el nombre de tu interfaz!!
 
$API = new routeros_api();
$API->debug = false;
if ($API->connect($ipRouteros , $Username , $Pass, $api_puerto)) {
       $API->write("/int/ether/getall",false);
       $API->write('?name='.$name,true);
       $READ = $API->read(false);
       $ARRAY = $API->parse_response($READ);
       if(count($ARRAY)>0){ // SI LA INTERFAZ EXISTE!!
            $id_interfaz = $ARRAY[0][".id"];
            $disabled = $ARRAY[0]["disabled"];
            if ($disabled=="true"){ // SI ESTA DESACTIVADA!!
                $API->write("/int/ether/enable",false);
                $API->write("=.id=".$id_interfaz,true);
                echo '<strong>'.$name.'</strong> esta ahora activada <img alt="" src="icon_led_green.png" /> ';
            }else{ // SI ESTA ACTIVADA!!
                $API->write("/int/ether/disable",false);
                $API->write("=.id=".$id_interfaz,true);
                echo '<strong>'.$name.'</strong> esta ahora desactivada <img alt="" src="icon_led_grey.png" /> ';
            }
            $READ = $API->read(false);
    }else{  //el servidor API esta of line
           echo 'Ocurrio un error: '.$ARRAY['!trap'][0]['message'];
    }
 
}else{
    echo "<span style="color: #ff0000;">La conexion ha fallado. Verifique si el Api esta activo.</span>";
}
$API->disconnect();
?>
 
jorgear
just joined
Posts: 1
Joined: Mon Apr 20, 2020 4:26 am

Re: Enable and Disable Ethernet ports

Mon Apr 20, 2020 4:28 am

hola buenas tarde consulto como se puede bloquear las interfaces o puertos ethernet del mikrotik,, en una franja horaria.. por ejemplo a la madrugada desde ya gracias

Who is online

Users browsing this forum: Google [Bot] and 24 guests