欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现telnet功能示例

程序员文章站 2023-11-23 15:52:34
复制代码 代码如下:

复制代码 代码如下:

<?php
class phptelnet {
    var $show_connect_error=1;

    var $use_usleep=0;  // change to 1 for faster execution
        // don't change to 1 on windows servers unless you have php 5
    var $sleeptime=125000;
    var $loginsleeptime=1000000;

    var $fp=null;
    var $loginprompt;

    var $conn1;
    var $conn2;

    /*
    0 = success
    1 = couldn't open network connection
    2 = unknown host
    3 = login failed
    4 = php version too low
    */
    function connect($server,$user,$pass) {
        $rv=0;
        $vers=explode('.',php_version);
        $needvers=array(4,3,0);
        $j=count($vers);
        $k=count($needvers);
        if ($k<$j) $j=$k;
        for ($i=0;$i<$j;$i++) {
            if (($vers[$i]+0)>$needvers[$i]) break;
            if (($vers[$i]+0)<$needvers[$i]) {
                $this->connecterror(4);
                return 4;
            }
        }

        $this->disconnect();

        if (strlen($server)) {
            if (preg_match('/[^0-9.]/',$server)) {
                $ip=gethostbyname($server);
                if ($ip==$server) {
                    $ip='';
                    $rv=2;
                }
            } else $ip=$server;
        } else $ip='127.0.0.1';

        if (strlen($ip)) {
            if ($this->fp=fsockopen($ip,23)) {
                fputs($this->fp,$this->conn1);
                $this->sleep();

                fputs($this->fp,$this->conn2);
                $this->sleep();
                $this->getresponse($r);
                $r=explode("\n",$r);
                $this->loginprompt=$r[count($r)-1];

                fputs($this->fp,"$user\n");
                $this->sleep();

                fputs($this->fp,"$pass\n");
                if ($this->use_usleep) usleep($this->loginsleeptime);
                else sleep(1);
                $this->getresponse($r);
                $r=explode("\n",$r);
                if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
                    $rv=3;
                    $this->disconnect();
                }
            } else $rv=1;
        }

        if ($rv) $this->connecterror($rv);
        return $rv;
    }

    function disconnect($exit=1) {
        if ($this->fp) {
            if ($exit) $this->docommand('exit',$junk);
            fclose($this->fp);
            $this->fp=null;
        }
    }

    function docommand($c,&$r) {
        if ($this->fp) {
            fputs($this->fp,"$c\n");
            $this->sleep();
            $this->getresponse($r);
            $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);
        }
        return $this->fp?1:0;
    }

    function getresponse(&$r) {
        $r='';
        do {
            $r.=fread($this->fp, 1024);
            $s=socket_get_status($this->fp);
        } while ($s['unread_bytes']);
    }

    function sleep() {
        if ($this->use_usleep) usleep($this->sleeptime);
        else sleep(1);
    }

    function phptelnet() {
        $this->conn1=chr(0xff).chr(0xfb).chr(0x1f).chr(0xff).chr(0xfb).
            chr(0x20).chr(0xff).chr(0xfb).chr(0x18).chr(0xff).chr(0xfb).
            chr(0x27).chr(0xff).chr(0xfd).chr(0x01).chr(0xff).chr(0xfb).
            chr(0x03).chr(0xff).chr(0xfd).chr(0x03).chr(0xff).chr(0xfc).
            chr(0x23).chr(0xff).chr(0xfc).chr(0x24).chr(0xff).chr(0xfa).
            chr(0x1f).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xff).
            chr(0xf0).chr(0xff).chr(0xfa).chr(0x20).chr(0x00).chr(0x33).
            chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2c).chr(0x33).
            chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xff).chr(0xf0).
            chr(0xff).chr(0xfa).chr(0x27).chr(0x00).chr(0xff).chr(0xf0).
            chr(0xff).chr(0xfa).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
            chr(0x45).chr(0x52).chr(0x4d).chr(0xff).chr(0xf0);
        $this->conn2=chr(0xff).chr(0xfc).chr(0x01).chr(0xff).chr(0xfc).
            chr(0x22).chr(0xff).chr(0xfe).chr(0x05).chr(0xff).chr(0xfc).chr(0x21);
    }

    function connecterror($num) {
        if ($this->show_connect_error) switch ($num) {
        case 1: echo '<br />[php telnet] <a href="//www.jb51.net/php-telnet/errors/fsockopen.php">connect failed: unable to open network connection</a><br />'; break;
        case 2: echo '<br />[php telnet] <a href="//www.jb51.net/php-telnet/errors/unknown-host.php">connect failed: unknown host</a><br />'; break;
        case 3: echo '<br />[php telnet] <a href="//www.jb51.net/php-telnet/errors/login.php">connect failed: login failed</a><br />'; break;
        case 4: echo '<br />[php telnet] <a href="//www.jb51.net/php-telnet/errors/php-version.php">connect failed: your server\'s php version is too low for php telnet</a><br />'; break;
        }
    }
}

?>