<?php

namespace app\models\mikrotik\terminal;

use Net_SSH2;
use yii\base\Model;
use Yii;
use app\models\mikrotik\MikrotikConnect;
use app\models\mikrotik\MikrotikCrypt;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

class Terminal extends Model {

    public $command;
    public $response;

    public function rules() {
        return [
            ['command', 'string'],
            ['response', 'string'],
            ['command', 'required'],
        ];
    }

    public function com($server, $cmd) {
        $connect = MikrotikConnect::Instance();
        $mcrypt = new MikrotikCrypt();
        foreach ($server as $info => $values) {
            if (($values['pwd'] != '')) {
                $values['pwd'] = trim($mcrypt->Decrypt($values['pwd'], pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3")));
            }
            $ssh = new Net_SSH2($info);
            if (!$ssh->login($values['login'], $values['pwd'])) {
                return('ssh Login Failed');
            }
//            $ssh->read('[admin@MikroTik] >  ');
//            $ssh->write("system clock print\n");
//            $ret = $ssh->read();
//            return $ret;
            $ret = $ssh->exec($cmd);

            return $ret;
        }
    }

}
