php怎麼實作telnet

藏色散人
發布: 2023-03-13 12:06:01
原創
2263 人瀏覽過

php实现telnet的方法:1、创建一个PHP示例文件;2、通过“class PHPTelnet {function Connect($server,$user,$pass) {...}}”方式实现telnet功能即可。

php怎麼實作telnet

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php怎么实现telnet?

php实现telnet功能示例

代码如下:

<?php
class PHPTelnet {
    var $show_connect_error=1;
    var $use_usleep=0;  // change to 1 for faster execution
        // don&#39;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&#39;t open network connection
    2 = unknown host
    3 = login failed
    4 = PHP version too low
    */
    function Connect($server,$user,$pass) {
        $rv=0;
        $vers=explode(&#39;.&#39;,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(&#39;/[^0-9.]/&#39;,$server)) {
                $ip=gethostbyname($server);
                if ($ip==$server) {
                    $ip=&#39;&#39;;
                    $rv=2;
                }
            } else $ip=$server;
        } else $ip=&#39;127.0.0.1&#39;;
        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]==&#39;&#39;)||($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(&#39;exit&#39;,$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=&#39;&#39;;
        do { 
            $r.=fread($this->fp, 1024);
            $s=socket_get_status($this->fp);
        } while ($s[&#39;unread_bytes&#39;]);
    }
    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 &#39;<br />[PHP Telnet] <a href="https://www.jb51.net/php-telnet/errors/fsockopen.php">Connect failed: Unable to open network connection</a><br />&#39;; break;
        case 2: echo &#39;<br />[PHP Telnet] <a href="https://www.jb51.net/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a><br />&#39;; break;
        case 3: echo &#39;<br />[PHP Telnet] <a href="https://www.jb51.net/php-telnet/errors/login.php">Connect failed: Login failed</a><br />&#39;; break;
        case 4: echo &#39;<br />[PHP Telnet] <a href="https://www.jb51.net/php-telnet/errors/php-version.php">Connect failed: Your server\&#39;s PHP version is too low for PHP Telnet</a><br />&#39;; break;
        }
    }
}
?>
登入後複製

推荐学习:《PHP视频教程

以上是php怎麼實作telnet的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!