文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

php怎么实现telnet

2019-05-07 08:11

关注

本文操作环境: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'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;
    
    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="https://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="https://www.jb51.net/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a><br />'; break;
        case 3: echo '<br />[PHP Telnet] <a href="https://www.jb51.net/php-telnet/errors/login.php">Connect failed: Login failed</a><br />'; break;
        case 4: echo '<br />[PHP Telnet] <a href="https://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;
        }
    }
}
?>

推荐学习:《PHP视频教程》

以上就是php怎么实现telnet的详细内容,更多请关注编程界其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯