Follow us on...
Follow us on G+ Follow us on Twitter Follow us on Facebook Watch us on YouTube
Register
Results 1 to 3 of 3
  1. #1
    Status
    Offline
    asahib1's Avatar
    Baru Gabung
    Join Date
    Jan 2010
    Posts
    2
    Reviews
    Read 0 Reviews
    Downloads
    0
    Uploads
    0
    Feedback Score
    0

    Bagaimana caranya connect modified API ke mikrotik

    dear brother n sister..
    saya baru2 ini memakai router os mikrotik via VMware di win32 XPsp2.
    lagi coba2 script modified API mikrotik dengan basis PHP.. nah yang saya bingung bagaimana mengkoneksikan script API ini ke mikrotik?
    padahal ip-nya udah diarahkan ke ipnya mikrotik e0 yg bridging ke phisical ethernet oleh vmware.
    beberapa kali coba, masih belum bisa connect.
    (percobaanya saya pakai httpserver Apache dgn PHP5 buat menjalankan php scriptnya, dengan database MySql)
    masalahnya bahasa inggris saya kurang bagus, jadi kalau ke forum mikrotik.wiki agak2 gimana gitu..
    dimohon bantuannya..

    ini saya lampirkan scriptnya.
    mohon maaf kalau kepanjangan scriptnya..
    <? require("routeros.class.php");

    $API = new RouterOS();

    //$API->debug = true;

    //$perintah = $_REQUEST["text1"];
    $usr = $_REQUEST["text1"];
    $pwd = $_REQUEST["text2"];

    //echo $perintah;

    if (!$API->connect('192.168.0.1', '$usr', '$pwd')) {

    //buat nulis perintah ke router jika connect
    //$API->writeSock($perintah);
    //jika tidak connect
    echo "<script>alert('gagal connect');</script>";
    die("<script>location.href='index.php';</script>");
    }
    class RouterOS
    {
    private $sock;
    private $where;

    private $tags = array();
    private $tagIndex = 1;
    private $dispatcher = array();

    public $readOnly = FALSE;

    private function writeSock($cmd = '') {
    //if(strlen($cmd) == 0)
    // echo "<<< ---\n";
    //else
    // echo "<<< $cmd\n";

    $l = strlen($cmd);
    if($l < 0x80) {
    fwrite($this->sock, chr($l));
    }
    else if($l < 0x4000) {
    $l |= 0x8000;
    fwrite($this->sock, chr(($l >> 8) & 0xFF) . chr($l & 0xFF));
    }
    else if($l < 0x200000) {
    $l |= 0xC00000;
    fwrite($this->sock, chr(($l >> 16) & 0xFF) . chr(($l >> 8) & 0xFF) . chr($l & 0xFF));
    }
    else if($l < 0x10000000) {
    $l |= 0xE0000000;
    fwrite($this->sock, chr(($l >> 24) & 0xFF) . chr(($l >> 16) & 0xFF) . chr(($l >> 8) & 0xFF) . chr($l & 0xFF));
    }
    else {
    fwrite($this->sock, chr(0xF0) . chr(($l >> 24) & 0xFF) . chr(($l >> 16) & 0xFF) . chr(($l >> 8) & 0xFF) . chr($l & 0xFF));
    }

    fwrite($this->sock, $cmd);
    }

    private function readSock() {
    $c = ord(fread($this->sock, 1));
    if(($c & 0x80) == 0x00) {
    }
    else if(($c & 0xC0) == 0x80) {
    $c &= ~0xC0;
    $c = ($c << 8) + ord(fread($this->sock, 1));
    }
    else if(($c & 0xE0) == 0xC0) {
    $c &= ~0xE0;
    $c = ($c << 8) + ord(fread($this->sock, 1));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    }
    else if(($c & 0xF0) == 0xE0) {
    $c &= ~0xF0;
    $c = ($c << 8) + ord(fread($this->sock, 1));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    }
    else {
    $c = ord(fread($this->sock));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    $c = ($c << 8) + ord(fread($this->sock, 1));
    }

    if($c == 0) {
    //echo ">>> ---\n";
    return NULL;
    }

    $o = '';
    while(strlen($o) < $c)
    $o .= fread($this->sock, $c - strlen($o));

    //echo ">>> $o\n";
    return $o;
    }

    private function trap($args, $hide = FALSE) {
    if($args['message'] && $args['message'] != 'interrupted')
    echo "trap[".$this->where."]: ${args['message']}\n";
    while($this->response() != '!done');
    }

    static function connect($host, $login, $password, $port = 8728, $timeout = 10) {
    $self = new RouterOS();

    // open socket
    if(($self->sock = @fsockopen($host, 8728, $errno, $errstr, $timeout)) === FALSE)
    return NULL;
    stream_set_timeout($self->sock, $timeout);

    // initiate login
    $self->send('', 'login');
    $type = $self->response(&$args);
    if($type != '!done' || !isset($args['ret']))
    return NULL;

    // try to login
    $self->send('', 'login', FALSE, array('name' => $login, 'response' => '00'.md5(chr(0).$password.pack('H*',$args['ret']))));
    $type = $self->response(&$args);
    if($type == '!done')
    return $self;
    else if($type == '!trap')
    $self->trap($args);
    unset($self);
    return NULL;
    }

    public function setTimeout($timeout = 5) {
    return stream_set_timeout($this->sock, $timeout);
    }

    private function send($cmd, $type, $proplist = FALSE, $args = FALSE, $tag = FALSE) {
    $result = TRUE;

    if(is_array($cmd))
    $cmd = '/' . join('/', $cmd);
    $cmd .= '/' . $type;

    $this->where = $cmd;

    // send command & args
    $this->writeSock($cmd);
    if($args) {
    foreach($args as $key=>$value)
    $this->writeSock("=$key=". $value);
    }
    if($proplist)
    $this->writeSock(".proplist=" . (is_array($proplist) ? join(',', $proplist) : $proplist));
    if($tag) {
    if(is_callable($tag)) {
    $result = $this->tagIndex++;
    $this->tags[$result] = $tag;
    }
    else {
    $result = $tag;
    }
    $this->writeSock(".tag=$result");
    }
    $this->writeSock();

    return $result;
    }

    private function response($args = FALSE, $dispatcher = FALSE) {
    if($dispatcher && count($this->dispatcher)) {
    $res = array_shift($this->dispatcher);
    $args = $res["args"];
    return $res["type"];
    }

    while(true) {
    $args = array();
    $type = FALSE;

    // read response type
    if($type = $this->readSock()) {
    if($type[0] != '!') {
    while($this->readSock());
    return FALSE;
    }
    }

    // read response parameters
    while($line = $this->readSock()) {
    if($line[0] == '=') {
    $line = explode('=', $line, 3);
    $args[$line[1]] = count($line) == 3 ? $line[2] : TRUE;
    continue;
    }
    else {
    $line = explode('=', $line, 2);
    $args[$line[0]] = isset($line[1]) ? $line[1] : '';
    }
    }
    unset($args['debug-info']);

    if(isset($args[".tag"])) {
    if($dispatcher)
    return $type;
    $this->dispatcher[] = array("tag" => $args[".tag"], "type" => $type, "args" => $args);
    }
    else {
    return $type;
    }
    }
    return FALSE;
    }

    function getall($cmd, $proplist = FALSE, $args = array(), $assoc = FALSE, $callback = FALSE) {
    $res = $this->send($cmd, 'getall', $proplist, $args, $callback);

    if($proplist) {
    if(!is_array($proplist))
    $proplist = explode(',', $proplist);
    $proplist = array_fill_keys($proplist, TRUE);
    }

    if($callback) {
    return $res;
    }

    $ids = array();

    // wait for response
    while(true) {
    $ret = array();
    switch($type = $this->response(&$ret)) {
    case '!re':
    if($proplist)
    $ret = array_intersect_key($ret, $proplist);
    if(isset($ret['.id']))
    if($assoc)
    $ids[$ret[$assoc]] = $ret;
    else
    $ids[] = $ret;
    else
    foreach($ret as $key => $value)
    $ids[$key] = $value;
    break;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    case '!done':
    break 2;

    default:
    die("getall: undefined type=$type\n");
    }
    }

    return $ids;
    }

    function set($cmd, $args = array(), $callback = FALSE) {
    if($this->readOnly)
    return TRUE;

    $res = $this->send($cmd, 'set', FALSE, $args, $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("set: undefined type\n");
    }
    }

    function reboot() {
    $this->send('/system', 'reboot', FALSE, FALSE);

    echo "!! rebooting...\n";
    sleep(5);

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    return TRUE;
    }
    }

    function cancel($tag = FALSE, $callback = FALSE) {
    if(is_callable($tag)) {
    $tag = array_search($tag, $this->tags);
    if($tag === FALSE) {
    echo "cancel: undefined tag\n";
    return FALSE;
    }
    }

    $res = $this->send('', 'cancel', FALSE, array(".tag" => $tag), FALSE, $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("set: undefined type\n");
    }
    }

    function fetchurl($url, $callback = FALSE) {
    $finished = FALSE;

    echo ".. downloading $url\n";

    $res = $this->send('/tool', 'fetch', FALSE, array('url' => $url), $callback);

    if($callback) {
    return $res;
    }

    while(true) {
    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    case '!re':
    switch($ret['status']) {
    case 'connecting':
    case 'requesting':
    break;

    case 'downloading':
    if(!$ret['total'])
    break;
    $progress = round(intval($ret['downloaded'])*100 / intval($ret['total']), 1);
    echo ".. ${ret['downloaded']} of ${ret['total']} ($progress%) within ${ret['duration']}\n";
    break;

    case 'finished':
    echo ".. downloaded!\n";
    $this->cancel();
    $finished = TRUE;
    break;

    case 'failed':
    echo "!! failed!\n";
    $this->cancel();
    break;

    default:
    die("fetch: undefined response (${ret['status']})\n");
    }
    break;

    default:
    die("fetch: undefined type\n");
    }
    flush();
    }
    return $finished;
    }

    function move($cmd, $id, $before, $callback = FALSE) {
    if($this->readOnly)
    return TRUE;

    $res = $this->send($cmd, 'move', FALSE, array('numbers' => $id, 'destination' => $before), $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("set: undefined type\n");
    }
    }

    function add($cmd, $args = array(), $callback = FALSE) {
    if($this->readOnly)
    return TRUE;

    $res = $this->send($cmd, 'add', FALSE, $args, $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    if(isset($ret['ret']))
    return $ret['ret'];
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("set: undefined type\n");
    }
    }

    function remove($cmd, $id, $callback = FALSE) {
    if($this->readOnly)
    return TRUE;

    $res = $this->send($cmd, 'remove', FALSE, array('.id' => is_array($id) ? join(',', $id) : $id), $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("remove: undefined type\n");
    }
    }

    function unsett($cmd, $id, $value, $callback = FALSE) {
    if($this->readOnly)
    return TRUE;

    $res = $this->send($cmd, 'unset', FALSE, array('numbers' => $id, 'value-name' => $value), $callback);

    if($callback) {
    return $res;
    }

    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("unset: undefined type\n");
    }
    }

    function scan($id, $duration="00:02:00", $callback = FALSE) {
    $res = $this->send('/interface/wireless', 'scan', FALSE, array('.id' => $id, 'duration' => $duration), $callback);

    if($callback) {
    return $res;
    }

    $results = array();

    while(true) {
    $ret = array();
    switch($type = $this->response(&$ret)) {
    case '!done':
    return $results;

    case '!re':
    $results[$ret['address']] = $ret;
    break;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("scan: undefined type: $type\n");
    }
    }
    }

    function btest($address, $speed = "1M", $protocol = "tcp", $callback = FALSE) {
    list($proto, $count) = explode(":", $protocol, 2);

    $args = array(
    "address" => $address,
    "direction" => "transmit",
    "local-tx-speed" => $speed);

    if($proto == "tcp") {
    $count = min(max(intval($count), 1), 20);
    $args["protocol"] = "tcp";
    $args["tcp-connection-count"] = $count;
    }
    else if($proto == "udp") {
    $count = min(max(intval($count), 30), 1500);
    $args["protocol"] = "udp";
    $args["local-udp-tx-size"] = $count;
    }
    else {
    die("invalid protocol: $proto\n");
    }

    $res = $this->send('/tool', 'bandwidth-test', FALSE, $args, $callback);

    //echo ".. running btest[$res] to $address ($speed/$protocol)...\n";

    if($callback) {
    return $res;
    }

    while(true) {
    $ret = array();
    switch($type = $this->response(&$ret)) {
    case '!done':
    return TRUE;

    case '!re':
    print_r($ret);
    break;

    case '!trap':
    $this->trap($ret);
    return FALSE;

    default:
    die("btest: undefined type: $type\n");
    }
    }
    }

    function dispatch(&$continue) {
    while($continue || count($this->tags)) {
    switch($type = $this->response(&$ret, TRUE)) {
    case '!re':
    if(isset($ret['.tag'])) {
    $callback = $this->tags[$ret['.tag']];
    if(is_callable($callback))
    $callback($this, TRUE, $ret);
    }
    break;

    case '!done':
    if(isset($ret['.tag'])) {
    $callback = $this->tags[$ret['.tag']];
    if(is_callable($callback))
    call_user_func($callback, $this, TRUE, NULL);
    unset($this->tags[$ret['.tag']]);
    }
    break;

    case '!trap':
    if(isset($ret['.tag'])) {
    $callback = $this->tags[$ret['.tag']];
    if(is_callable($callback))
    call_user_func($callback, $this, FALSE, $ret);
    unset($this->tags[$ret['.tag']]);
    }
    break;

    default:
    die("dispatch: undefined type\n");
    }
    }

    return count($this->tags) != 0;
    }
    };

    ?>

    ?>

  2. #2
    Status
    Offline
    itik_kecil's Avatar
    Baru Gabung
    Join Date
    Oct 2009
    Posts
    12
    Reviews
    Read 0 Reviews
    Downloads
    0
    Uploads
    0
    Feedback Score
    0
    pake mikrotik Versi brp? trus Api service yg di dalam IP service sudah di enable belom..?
    ane pake mikrotikROS v3.1 Apinya bisa konek kok

  3. #3
    Status
    Offline
    fatchur8710's Avatar
    Baru Gabung
    Join Date
    Mar 2013
    Posts
    10
    Reviews
    Read 0 Reviews
    Downloads
    0
    Uploads
    0
    Feedback Score
    0
    kalau di pasang di hosting settingnya gimana gan ?

 

 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Gimana Caranya VirtualBox bisa Connect dgn Mikrotik
    By arovah in forum General Networking
    Replies: 10
    Last Post: 21-04-2012, 13:31
  2. Replies: 11
    Last Post: 01-09-2009, 05:25
  3. (Help) Mikrotik KU gak bisa connect
    By syahied in forum Beginner Basics
    Replies: 29
    Last Post: 29-02-2008, 16:02
  4. [ask]gmn caranya supaya antar 2 antenna bs connect?
    By dedinho78 in forum Beginner Basics
    Replies: 2
    Last Post: 29-02-2008, 15:28
  5. [Ask]Remote pc lewat mikrotik, gimana caranya ya :(
    By lylyk in forum Beginner Basics
    Replies: 4
    Last Post: 04-11-2007, 09:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •