Community discussions

MikroTik App
 
gebgebdmz
just joined
Topic Author
Posts: 1
Joined: Tue Nov 17, 2020 2:57 pm

How to send mikrotik command result to php

Wed Nov 18, 2020 12:54 pm

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 instance "tester"), but I cant send the result from mikrotik(example: [/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 same ip pulblic from mikrotik and database
    		if($ms[0][5]== $remote){
    			// echo "ok";
    			exit();	
    		}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.

Who is online

Users browsing this forum: Bing [Bot] and 33 guests