Community discussions

MikroTik App
 
xezen
Long time Member
Long time Member
Topic Author
Posts: 628
Joined: Fri May 30, 2008 10:23 am
Location: South Africa

php code clean up help please

Tue Oct 21, 2014 7:59 pm

i run this code 2 times but i want to clean it up run it one time with 2 ip

192.168.87.1
and
192.168.87.2

can someone help me with this maybe on how to do it 2 set 2 values for 1 verable

<?php require_once('api_mt_include2.php'); ?>
<?php
$ipRouteros="192.168.87.1"; // tu RouterOS.
$Username="apilinks";
$Pass="api";
$api_puerto=8728;

$API = new routeros_api();
$API->debug = false;
if ($API->connect($ipRouteros , $Username , $Pass, $api_puerto)) {
$API->write("/system/ident/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$name = $ARRAY[0]["name"];
if(count($ARRAY)>0){ // si esta conectado
$API->write("/system/licen/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$nlevel = $ARRAY[0]["nlevel"];
$API->write("/system/reso/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$cpu = $ARRAY[0]["cpu"];
$cpu_frequency = $ARRAY[0]["cpu-frequency"];
$arquitectura = $ARRAY[0]["board-name"];
$API->write("/system/pack/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$version = $ARRAY[0]["version"];

echo '<img src="icon_led_green.png" />&nbsp;';
echo "<strong>".$name."(" .$arquitectura. ")</strong>&nbsp;&nbsp;";
echo "v:" . $version. "&nbsp;&nbsp;";
echo "level:" . $nlevel . "&nbsp;&nbsp;";
echo $cpu."(".$cpu_frequency." Mhz.)";
}else{ //el servidor API esta of line
echo '<img src="icon_led_grey.png" />&nbsp;'.$ARRAY['!trap'][0]['message'];
}

}else{
echo "<font color='#ff0000'>La conexion ha fallado. Verifique si el Api esta activo.</font>";
}
$API->disconnect();

echo

$ipRouteros="192.168.87.2";
$Username="apilinks";
$Pass="api";
$api_puerto=8728;

$API = new routeros_api();
$API->debug = false;
if ($API->connect($ipRouteros , $Username , $Pass, $api_puerto)) {
$API->write("/system/ident/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$name = $ARRAY[0]["name"];
if(count($ARRAY)>0){ // si esta conectado
$API->write("/system/licen/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$nlevel = $ARRAY[0]["nlevel"];
$API->write("/system/reso/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$cpu = $ARRAY[0]["cpu"];
$cpu_frequency = $ARRAY[0]["cpu-frequency"];
$arquitectura = $ARRAY[0]["board-name"];
$API->write("/system/pack/getall",true);
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$version = $ARRAY[0]["version"];

echo '<img src="icon_led_green.png" />&nbsp;';
echo "<strong>".$name."(" .$arquitectura. ")</strong>&nbsp;&nbsp;";
echo "v:" . $version. "&nbsp;&nbsp;";
echo "level:" . $nlevel . "&nbsp;&nbsp;";
echo $cpu."(".$cpu_frequency." Mhz.)";
}else{ //el servidor API esta of line
echo '<img src="icon_led_grey.png" />&nbsp;'.$ARRAY['!trap'][0]['message'];
}

}else{
echo "<font color='#ff0000'>La conexion ha fallado. Verifique si el Api esta activo.</font>";
}
$API->disconnect();
?>
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: php code clean up help please

Tue Oct 21, 2014 8:12 pm

Write the credentials in an array, and loop over it.

So instead of
<?php
$ipRouteros="192.168.87.1"; // tu RouterOS.
$Username="apilinks";
$Pass="api";
$api_puerto=8728;

//Junk #1

$ipRouteros="192.168.87.2";
$Username="apilinks";
$Pass="api";
$api_puerto=8728;

//Junk #2
Make that
<?php
$routers = array(
    array('192.168.87.1', 'apilinks', 'api', 8728),
    array('192.168.87.2', 'apilinks', 'api', 8728)
);
foreach ($routers as $router) {
    $ipRouteros = $router[0];
    $Username = $router[1];
    $Pass = $router[2];
    $api_puerto = $router[3];

    //Junk
}
 
xezen
Long time Member
Long time Member
Topic Author
Posts: 628
Joined: Fri May 30, 2008 10:23 am
Location: South Africa

Re: php code clean up help please

Wed Oct 22, 2014 10:30 pm

thanks for the help

Who is online

Users browsing this forum: BenceLK, Bing [Bot] and 62 guests