Как отправить результат команды mikrotik на php

#php #mikrotik

#php #mikrotik

Вопрос:

Я создал скрипт и php-файл, в котором он будет извлекать определенную ссылку, чтобы получить общедоступный адрес адреса mikrotik. Я могу отправить обычный текст (в данном случае «тестер»), но я не могу отправить результат из mikrotik ([/system identity print]).

Вот файл: скрипт mikrotik:

 :global name [/system identity print]
:global lock "tester"
# put it all together ----------------------------------------------------
:set $str "key=$lockamp;namepr=$name"

# send to server ---------------------------------------------------------

:do {
                 :put "Checking in";

                /tool fetch mode=https  keep-result=no http-method=post   url="https://sc.glcnetworks.com:63010/scriptcase/app/glcbilling/receive_monitoring_from_host/?$str"  http-data=$str ; 
                  :put "Data sended successfully";
 
} on-error={ log warning "Greeter: Send to server Failed!" }
  

php-файл из scriptcase:

 #get hostname,ip public and key from mikrotik
$hostname_get   = $_GET['namepr'];
$key_get        = $_GET['key'];
$remote         = $_SERVER['REMOTE_ADDR'];

// Check for record

$check_sql = "SELECT id,hostname"
   . " FROM glc_host"
   . " WHERE hostname = '$hostname_get'";
sc_lookup(rs, $check_sql);

// Check for record
$check_sql2 = "SELECT key,value"
   . " FROM bas_config"
   . " WHERE key = 'mikrotik_secrets_$hostname_get'";
sc_lookup(ls, $check_sql2);

$id = isset({rs[0][0]});
// Check for record, to record public IP changes or new public ip changes
$check_sql3 = "SELECT *"
   . " FROM glc_host_monitoring"
   . " WHERE host_id = '$id'";
sc_lookup(ms, $check_sql3);

#to check for mikrotik output, i send the result to activity log
sc_log_add("test", "$key_get and $hostname_get");
#if key and hostname match from database
if (isset({rs[0][1]}) amp;amp; isset({ls[0][1]})){
# if record exist 
    if(isset({rs[0][0]})== isset({ms[0][1]})){
        // echo "yes" ;
#no change happen
        if($ms[0][5]== $remote){
            // echo "ok";
            exit();
#ip public changed          
        }else{
            echo "update";
            $update_table  = 'glc_host_monitoring';      // Table name
            $update_where  = "host_id = '$id' "; // Where clause
            $update_fields = array(   // Field list, add as many as needed
                "ip_address_public = '$remote'",
            );

            // Update record
            $update_sql = 'UPDATE ' . $update_table
                . ' SET '   . implode(', ', $update_fields)
                . ' WHERE ' . $update_where;
            sc_exec_sql($update_sql);
        }
           
    }else{
#if record does not exist
        $insert_table  = 'glc_host_monitoring';      // Table name
        $insert_fields = array(   // Field list, add as many as needed
             'host_id' => "'$id'",
             'ip_route' => "'NULL'",
             'ip_address' => "'NULL'",
             'interface' => "'NULL'",
             'ip_address_public' => "'$remote'",
         );

        // Insert record
        $insert_sql = 'INSERT INTO ' . $insert_table
            . ' ('   . implode(', ', array_keys($insert_fields))   . ')'
            . ' VALUES ('    . implode(', ', array_values($insert_fields)) . ')';

        sc_exec_sql($insert_sql);
    
    }
}else{
#key and hostname not matched
    echo "failed";
}
  

Я искал везде, но не нашел правильного ответа, пожалуйста, скажите мне, что не так с моим файлом, я новичок в скриптинге mikrotik.

Ответ №1:

Попробуйте другой способ. Пусть сервер запрашивает информацию, а затем сохраняет ее в БД

https://wiki.mikrotik.com/wiki/API_PHP_package

 <?php
use PEAR2NetRouterOS;

$client = new RouterOSClient('192.168.88.1', 'admin', 'password');
$util = new RouterOSUtil($client);
$util->setMenu('/system identity')->get('name'); # Just guessing
...