找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 16826|回复: 12

[其它] 刚在网上看了个PHP,说是能与ROS API连接的,谁懂?

  [复制链接]
发表于 2010-12-2 08:43:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
本帖最后由 weikun444 于 2010-12-2 08:49 编辑

刚上网看到的,PHP,能不能再这个基础上改造一下,达到注册用户、用户自行更改密码、或找回密码的页面?

例子中的文件是返回全部PPPPOE用户信息的功能。

附上2个PHP文件内容:
index.php文件内容:

<?php

require('routeros_api.class.php');

$API = new RouterOS_api();

$API->debug = false;

if ($API->connect('192.168.1.1', 'admin', 'admin')) {

$API->write('/int/eth/getall');

   $ARRAY = $API->read();

   print_r($ARRAY);

   $API->disconnect();

}

?>

routeros_api.class.php文件内容:

<?php
//
// RouteROS API class
// Author: Denis Basta
//
// read() function altered by Nick Barnes to take into account the placing
// of the "!done" reply and also correct calculation of the reply length.
///
// read() function altered by Ben Menking (ben@infotechsc.com); removed
// echo statement that dumped byte data to screen
//
///////////////////////////
// Revised by: Jeremy Jefferson (http://jeremyj.com)
// January 8, 2010
//
//        Fixed write function in order to allow for queries to be executed
//

class routeros_api {

        var $debug = false;                        // Show debug information
        var $error_no;                                // Variable for storing connection error number, if any
        var $error_str;                                // Variable for storing connection error text, if any

        var $attempts = 5;                        // Connection attempt count
        var $connected = false;                // Connection state
        var $delay = 3;                                // Delay between connection attempts in seconds
        var $port = 8728;                        // Port to connect to
        var $timeout = 3;                        // Connection attempt timeout and data read timeout

        var $socket;                                // Variable for storing socket resource

        /**************************************************
         *
         *************************************************/

        function debug($text) {

                if ($this->debug)
                        echo $text . "\n";

        }

        /**************************************************
         *
         *************************************************/

        function encode_length($length) {

                if ($length < 0x80) {

                        $length = chr($length);

                }
                else
                if ($length < 0x4000) {

                        $length |= 0x8000;

                        $length = chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

                }
                else
                if ($length < 0x200000) {

                        $length |= 0xC00000;

                        $length = chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

                }
                else
                if ($length < 0x10000000) {

                        $length |= 0xE0000000;

                        $length = chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

                }
                else
                if ($length >= 0x10000000)
                        $length = chr(0xF0) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

                return $length;

        }

        /**************************************************
         *
         *************************************************/

        function connect($ip, $login, $password) {

                for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {

                        $this->connected = false;

                        $this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $ip . ':' . $this->port . '...');

                        if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout) ) {

                                socket_set_timeout($this->socket, $this->timeout);

                                $this->write('/login');

                                $RESPONSE = $this->read(false);

                                if ($RESPONSE[0] == '!done') {

                                        if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES) ) {

                                                if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {

                                                        $this->write('/login', false);
                                                        $this->write('=name=' . $login, false);
                                                        $this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1]) ) );

                                                        $RESPONSE = $this->read(false);

                                                        if ($RESPONSE[0] == '!done') {

                                                                $this->connected = true;

                                                                break;

                                                        }

                                                }

                                        }

                                }

                                fclose($this->socket);

                        }

                        sleep($this->delay);

                }

                if ($this->connected)
                        $this->debug('Connected...');
                else
                        $this->debug('Error...');

                return $this->connected;

        }

        /**************************************************
         *
         *************************************************/

        function disconnect() {

                fclose($this->socket);

                $this->connected = false;

                $this->debug('Disconnected...');

        }

        /**************************************************
         *
         *************************************************/

        function parse_response($response) {

                if (is_array($response) ) {

                        $PARSED = array();
                        $CURRENT = null;

                        for ($i = 0, $imax = count($response); $i < $imax; $i++) {

                                if (in_array($response[$i], array('!fatal', '!re', '!trap') ) ) {

                                        if ($response[$i] == '!re')
                                                $CURRENT = &$PARSED[];
                                        else
                                                $CURRENT = &$PARSED[$response[$i]][];

                                }
                                else
                                if ($response[$i] != '!done') {

                                        if (preg_match_all('/[^=]+/i', $response[$i], $MATCHES) )
                                                $CURRENT[$MATCHES[0][0]] = (isset($MATCHES[0][1]) ? $MATCHES[0][1] : '');

                                }

                        }

                        return $PARSED;

                }
                else
                        return array();

        }

        /**************************************************
         *
         *************************************************/

        function array_change_key_name(&$array) {
                if (is_array($array) ) {
                        foreach ($array as $k => $v) {
                                $tmp = "";
                                $tmp = str_replace("-","_",$k);
                                $tmp = str_replace("/","_",$tmp);
                                if ($tmp) {
                                        $array_new[$tmp] = $v;
                                } else {
                                        $array_new[$k] = $v;
                                }
                        }
                        return $array_new;
                } else {
                        return $array;
                }
        }

        /**************************************************
         *
         *************************************************/

        function parse_response4smarty($response) {
                if (is_array($response) ) {
                        $PARSED = array();
                        $CURRENT = null;
                        for ($i = 0, $imax = count($response); $i < $imax; $i++) {
                                if (in_array($response[$i], array('!fatal', '!re', '!trap') ) ) {
                                        if ($response[$i] == '!re')
                                                $CURRENT = &$PARSED[];
                                        else
                                                $CURRENT = &$PARSED[$response[$i]][];
                                }
                                else
                                if ($response[$i] != '!done') {
                                        if (preg_match_all('/[^=]+/i', $response[$i], $MATCHES) )
                                                $CURRENT[$MATCHES[0][0]] = (isset($MATCHES[0][1]) ? $MATCHES[0][1] : '');
                                }
                        }
                        foreach ($PARSED as $key => $value) {
                                $PARSED[$key] = $this->array_change_key_name($PARSED[$key]);
                        }
                        return $PARSED;
                }
                else {
                        return array();
                }
        }

        /**************************************************
         *
         *************************************************/

   function read($parse = true) {

      $RESPONSE = array();

      while (true) {

         // Read the first byte of input which gives us some or all of the length
         // of the remaining reply.
         $BYTE = ord(fread($this->socket, 1) );
         $LENGTH = 0;

         // If the first bit is set then we need to remove the first four bits, shift left 8
         // and then read another byte in.
         // We repeat this for the second and third bits.
         // If the fourth bit is set, we need to remove anything left in the first byte
         // and then read in yet another byte.
         if ($BYTE & 128) {
            if (($BYTE & 192) == 128) {
               $LENGTH = (($BYTE & 63) << 8 ) + ord(fread($this->socket, 1)) ;
            } else {
               if (($BYTE & 224) == 192) {
                  $LENGTH = (($BYTE & 31) << 8 ) + ord(fread($this->socket, 1)) ;
                  $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
               } else {
                  if (($BYTE & 240) == 224) {
                     $LENGTH = (($BYTE & 15) << 8 ) + ord(fread($this->socket, 1)) ;
                     $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
                     $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
                  } else {
                     $LENGTH = ord(fread($this->socket, 1)) ;
                     $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
                     $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
                     $LENGTH = ($LENGTH << 8 ) + ord(fread($this->socket, 1)) ;
                  }
               }
            }
         } else {
            $LENGTH = $BYTE;
         }

         // If we have got more characters to read, read them in.
         if ($LENGTH > 0) {
            $_ = "";
            $retlen=0;
            while ($retlen < $LENGTH) {
               $toread = $LENGTH - $retlen ;
               $_ .= fread($this->socket, $toread);
               $retlen = strlen($_);
            }
            $RESPONSE[] = $_ ;
            $this->debug('>>> [' . $retlen . '/' . $LENGTH . ' bytes read.');
         }

         // If we get a !done, make a note of it.
         if ($_ == "!done")
            $receiveddone=true;

         $STATUS = socket_get_status($this->socket);

         
         if ($LENGTH > 0)
            $this->debug('>>> [' . $LENGTH . ', ' . $STATUS['unread_bytes'] . '] ' . $_);

         if ( (!$this->connected && !$STATUS['unread_bytes']) ||
            ($this->connected && !$STATUS['unread_bytes'] && $receiveddone) )
            break;

      }

      if ($parse)
         $RESPONSE = $this->parse_response($RESPONSE);

      return $RESPONSE;

   }
        /**************************************************
         *
         *************************************************/

        function write($command, $param2 = true) {

                if ($command) {
                       
                        $data = explode("\n",$command);
                       
                        foreach ($data as $com) {
                                $com = trim($com);
                                fwrite($this->socket, $this->encode_length(strlen($com) ) . $com);
                                $this->debug('<<< [' . strlen($com) . '] ' . $com);
                        }

                       

                        if (gettype($param2) == 'integer') {

                                fwrite($this->socket, $this->encode_length(strlen('.tag=' . $param2) ) . '.tag=' . $param2 . chr(0) );

                                $this->debug('<<< [' . strlen('.tag=' . $param2) . '] .tag=' . $param2);

                        }
                        else
                        if (gettype($param2) == 'boolean')
                                fwrite($this->socket, ($param2 ? chr(0) : '') );

                        return true;

                }
                else
                        return false;

        }

}

?>


两个文件打包下载:
ros_php.rar (2.76 KB, 下载次数: 148)
routeros
发表于 2010-12-2 22:43:28 | 显示全部楼层
是的,搭建一个webserver,用网页的形式来修改routeros的配置!
routeros
回复

使用道具 举报

发表于 2010-12-2 23:40:21 | 显示全部楼层
要研究研究
routeros
回复

使用道具 举报

发表于 2010-12-3 18:36:32 | 显示全部楼层
这个得研究研究。
routeros
回复

使用道具 举报

发表于 2010-12-3 23:19:22 | 显示全部楼层
现在已经有很多个软件是通过api管理ros的了
routeros
回复

使用道具 举报

 楼主| 发表于 2010-12-4 10:20:42 | 显示全部楼层
关键是咱不懂PHP,没有PHP方面的知识。
routeros
回复

使用道具 举报

发表于 2010-12-10 16:14:48 | 显示全部楼层
routeros
回复

使用道具 举报

发表于 2011-1-30 09:05:06 | 显示全部楼层
如果站被拿下-那就悲剧了。
routeros
回复

使用道具 举报

发表于 2011-1-30 10:22:38 | 显示全部楼层
呵呵 API越来越方便了
routeros
回复

使用道具 举报

发表于 2012-9-8 08:16:47 | 显示全部楼层
方便是方便,前期工程不懂!
routeros
回复

使用道具 举报

发表于 2012-9-8 09:50:53 | 显示全部楼层
这个我已经开始用了.....根据上面的函数只能连接一台,改一下可以同时连接多台!
routeros
回复

使用道具 举报

发表于 2016-8-26 19:21:02 | 显示全部楼层
这个怎么改的兄弟们,正好现在我需要做这样的东西是不是放在集成环境上(wampserver),结果测试网页是白的啊,什么都没有了
routeros
回复

使用道具 举报

发表于 2018-5-24 13:38:15 | 显示全部楼层
喜欢这种API管理的
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-4-20 14:54 , Processed in 0.162221 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表