php实现telnet功能示例

 更新时间:2014年04月08日 09:58:03   作者:  
这篇文章主要介绍了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;

    /*
    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="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;
        }
    }
}

?>

相关文章

  • Thinkphp 框架基础之入口文件功能、定义与用法分析

    Thinkphp 框架基础之入口文件功能、定义与用法分析

    这篇文章主要介绍了Thinkphp 框架基础之入口文件功能、定义与用法,结合实例形式分析了Thinkphp入口文件基本功能、原理、定义与操作注意事项,需要的朋友可以参考下
    2020-04-04
  • php并发加锁示例

    php并发加锁示例

    本文介绍了php并发加锁示例,对数据进行加锁,只容许一个用户在一个时间内进行操作,这个时候就需要用到锁了,需要的朋友可以了解一下。
    2016-10-10
  • PHP图像识别技术原理与实现

    PHP图像识别技术原理与实现

    本篇文章主要介绍了PHP图像识别技术原理与实现,这个平时做的密码验证有异曲同工之处,有需要的可以了解一下。
    2016-10-10
  • PHP中通过trigger_error触发PHP错误示例

    PHP中通过trigger_error触发PHP错误示例

    这篇文章主要介绍了PHP中通过trigger_error触发PHP错误示例,本文介绍了错误抑制符@以及通过 trigger_error 触发 PHP 错误示例,需要的朋友可以参考下
    2015-06-06
  • 深入浅出php socket编程

    深入浅出php socket编程

    这篇文章主要介绍了深入浅出php socket编程,本文详细的讲解了socket的相关知识、PHP socket的编程实例行装内容,需要的朋友可以参考下
    2015-05-05
  • PHP处理SQL脚本文件导入到MySQL的代码实例

    PHP处理SQL脚本文件导入到MySQL的代码实例

    通常在制作安装程式,数据备份程序的时候会要用到这样的代码,我看网上有是有不太多,而且有些也不是很好用,有时候这种代码直接用现成的可以节省很多时间,那么我就从stackoverflow转了一个过来,需要的朋友可以参考下
    2014-03-03
  • PHP实现获取FLV文件的时间

    PHP实现获取FLV文件的时间

    这篇文章主要介绍了PHP实现获取FLV文件的时间,本文直接给出实现代码和使用方法,需要的朋友可以参考下
    2015-02-02
  • Laravel框架模板加载,分配变量及简单路由功能示例

    Laravel框架模板加载,分配变量及简单路由功能示例

    这篇文章主要介绍了Laravel框架模板加载,分配变量及简单路由功能,结合实例形式分析了Laravel框架模板加载,分配变量及简单路由功能相关原理、操作技巧与注意事项,需要的朋友可以参考下
    2018-06-06
  • ThinkPHP3.2利用QQ邮箱/163邮箱通过PHPMailer发送邮件的方法

    ThinkPHP3.2利用QQ邮箱/163邮箱通过PHPMailer发送邮件的方法

    最近因为工作的需要,要实现给我们的网站用户发送邮件的需求,所以下面这篇文章主要给大家介绍了关于ThinkPHP3.2利用QQ邮箱/163邮箱通过PHPMailer发送邮件的方法,需要的朋友可以参考借鉴,下面随着小编来一起来学习学习吧。
    2017-11-11
  • PHP水印类,支持添加图片、文字、填充颜色区域的实现

    PHP水印类,支持添加图片、文字、填充颜色区域的实现

    下面小编就为大家带来一篇PHP水印类,支持添加图片、文字、填充颜色区域的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02

最新评论