How to send mikrotik command result to php

1.1k Views Asked by At

I have created a script and a php file where it will fetch a certain link to get the public address of the mikrotik address. I can send a plain text(in this instancem "tester"), but I cant send the result from mikrotik([/system identity print]).

Here is the file: mikrotik script:

:global name [/system identity print]
:global lock "tester"
# put it all together ----------------------------------------------------
:set $str "key=$lock&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 file from 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]}) && 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";
}

I have searched everywhere but I dont find the right answer, please tell me whats wrong with my file, Im new with mikrotik scripting.

1

There are 1 best solutions below

0
Facty On

Try it another way. Let server ask for info and then store it in DB

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

<?php
use PEAR2\Net\RouterOS;

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