Enable and Disable Ethernet ports
RouterOS Scripting and API

4 posts   •   Page 1 of 1
User avatar
Vyrtu
newbie
 
Posts: 45
Joined: Thu Oct 03, 2013 11:49 am
Location: Torrelavega,Spain

Enable and Disable Ethernet ports

by Vyrtu » 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 :(
Code: Select all
if ($API->connect($ipRouteros , $username , $pass, $api_puerto)) {
   {   
   $API->write('/interface/ethernet',false);
   $API->write('=disable=ether4',true);
   }
}


Code: Select all
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.
Noob with Pear2 API :(

User avatar
boen_robot
Forum Guru
Forum Guru
 
Posts: 1317
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Enable and Disable Ethernet ports

by boen_robot » 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:
Code: Select all
$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.
PEAR2_Net_RouterOS(1.0.0b5) - My API client in PHP
(Karma? If you want... no pressure...)

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

Re: Enable and Disable Ethernet ports

by Vyrtu » Thu Oct 03, 2013 12:27 pm

Thanks, but i still having problems. It's my full code:

Code: Select all
<?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();
?>
Noob with Pear2 API :(

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

Re: Enable and Disable Ethernet ports

by Vyrtu » Mon Oct 14, 2013 12:59 pm

My problem is solved. I post the final script.

Code: Select all
<?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();
?>
Noob with Pear2 API :(

4 posts   •   Page 1 of 1

Who is online

Users browsing this forum: Baidu [Spider] and 13 guests

It is currently Sat Dec 20, 2014 5:57 am