千家信息网

php如何实现telnet

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,这篇文章主要介绍了php如何实现telnet,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。php实现telnet的方法:1、创建一个P
千家信息网最后更新 2025年01月19日php如何实现telnet

这篇文章主要介绍了php如何实现telnet,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

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

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

php怎么实现telnet?

php实现telnet功能示例

代码如下:

$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 '
[PHP Telnet] Connect failed: Unable to open network connection
'; break; case 2: echo '
[PHP Telnet] Connect failed: Unknown host
'; break; case 3: echo '
[PHP Telnet] Connect failed: Login failed
'; break; case 4: echo '
[PHP Telnet] Connect failed: Your server\'s PHP version is too low for PHP Telnet
'; break; } }}?>

感谢你能够认真阅读完这篇文章,希望小编分享的"php如何实现telnet"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

0