0

我正在为我的应用程序使用以下类,它工作得非常好:

class Telnet
{
    /* (c) thies@thieso.net */

    var $sock = null;

    function telnet($host, $port)
    {
        $this->sock = fsockopen($host, $port);
        socket_set_timeout($this->sock, 2, 0);
    }

    function close()
    {
        if ($this->sock)
            fclose($this->sock);
        $this->sock = null;
        return "Disconnected";
    }

    function write($buffer)
    {
        $buffer = str_replace(chr(255), chr(255) . chr(255), $buffer);
        fwrite($this->sock, $buffer);
    }

    function getc()
    {
        return fgetc($this->sock);
    }

    function read_till($what)
    {
        $buf = '';
        while (1) {
            $IAC = chr(255);

            $DONT = chr(254);
            $DO = chr(253);

            $WONT = chr(252);
            $WILL = chr(251);

            $theNULL = chr(0);

            $c = $this->getc();

            if ($c === false)
                return nl2br($buf);

            if ($c == $theNULL) {
                continue;
            }

            if ($c == "\021") {
                continue;
            }

            if ($c != $IAC) {
                $buf .= $c;

                if ($what == (substr($buf, strlen($buf) - strlen($what)))) {
                    return nl2br($buf);
                } else {
                    continue;
                }
            }

            $c = $this->getc();

            if ($c == $IAC) {
                $buf .= $c;
            } else
                if (($c == $DO) || ($c == $DONT)) {
                    $opt = $this->getc();
                    //echo "we wont ".ord($opt)."\n";
                    fwrite($this->sock, $IAC . $WONT . $opt);
                } elseif (($c == $WILL) || ($c == $WONT)) {
                    $opt = $this->getc();
                    //echo "we dont ".ord($opt)."\n";
                    fwrite($this->sock, $IAC . $DONT . $opt);
                } else {
                    //echo "where are we? c=".ord($c)."\n";
                }
        }

    }
}

然后我为 CLI 添加了另一个页面(它将通过 jquery 与此类进行交互):

<html>
  <head>
    <title>Simple CLI</title>
    <script src="./../../script/jquery-1.7.1.min.js"></script>
  </head>
  <body>
    <div>
      User:<input id="username" type="text" />Password:<input id="password" type="text" />
      <input id="connect" type="button" value="Connect" />
      <div id="out">Welcome: Type 'connect' to connect to the terminal server.<br /></div>
      <input id="in" type="text" />
      <input id="disconnect" type="button" value="Disconnect" />
    </div>
    <script type="text/javascript">
      $('#connect').click(function(e){
        var user = $('#username').val();
        var pass = $('#password').val();
        if( user != "" && pass != "" ){
            $.post('abc.php', {cmd:"connect",user:user,pass:pass}, function(data) {
              $('#out').append(data);
            });
        }
      });
      $('#disconnect').click(function(e){
        $.post('abc.php', {cmd:"disconnect"}, function(data) {
          $('#out').append(data);
        });
      });
      $('#in').keyup(function(e) {
        if (e.which !== 13)
          return; 
        var cmd = { cmd: $(this).val() };
        $(this).val('');
        $.post('abc.php', cmd, function(data) {
          $('#out').append(data);
        });
      });
    </script>
  </body>
</html>

该类由以下人员使用:

if($_POST['cmd']){
    $command = $_POST['cmd'];
    switch($command){
        case 'connect':
            $output = '';
            if( !empty($_POST['user']) && !empty($_POST['pass']) ){
                $tn = new telnet("127.0.0.1", 23);
                $output = $tn->read_till("ogin:");
                $tn->write($_POST['user'] . "\r\n");
                $output .= $tn->read_till("word: ");
                $tn->write($_POST['pass'] . "\r\n");
                $output .= $tn->read_till(":> ");
                echo $output;                
            }
            break;
        case 'disconnect':
            echo $tn->close();
            break;
        default:
            $tn->write($command."\r\n");
            echo $tn->read_till(":> ");
            break;
    }
}

问题是我如何以及在哪里保存类对象,以便每当我通过 jquery 调用类对象时,它应该具有相同的对象,而不是每个命令都会首先打开与 telnet 服务器的新连接。

4

0 回答 0