<?php
class RouterosAPI{
    private $socket=null;
    public function connect($ip,$user,$pass,$port=8728){
        $this->socket=@fsockopen($ip,$port,$errno,$errstr,5);
        if(!$this->socket) return false;
        fwrite($this->socket,"/login\n");
        fwrite($this->socket,"=name=$user\n");
        fwrite($this->socket,"=password=$pass\n\n");
        return true;
    }
    public function comm($command,$params=[]){
        if(!$this->socket) return false;
        $cmd=$command;
        foreach($params as $k=>$v) $cmd.=" =$k=$v";
        $cmd.="\n";
        fwrite($this->socket,$cmd);
        fwrite($this->socket,"\n");
        return true;
    }
    public function disconnect(){ if($this->socket){ fclose($this->socket); $this->socket=null; } }
}
