php实现telnet功能示例


Posted in PHP onApril 08, 2014
<?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://3water.com/php-telnet/errors/fsockopen.php">Connect failed: Unable to open network connection</a><br />'; break;
        case 2: echo '<br />[PHP Telnet] <a href="https://3water.com/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a><br />'; break;
        case 3: echo '<br />[PHP Telnet] <a href="https://3water.com/php-telnet/errors/login.php">Connect failed: Login failed</a><br />'; break;
        case 4: echo '<br />[PHP Telnet] <a href="https://3water.com/php-telnet/errors/php-version.php">Connect failed: Your server\'s PHP version is too low for PHP Telnet</a><br />'; break;
        }
    }
}
?>
PHP 相关文章推荐
php下实现一个阿拉伯数字转中文数字的函数
Jul 10 PHP
发款php蜘蛛统计插件只要有mysql就可用
Oct 12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(七)
Jun 23 PHP
php删除指定目录的方法
Apr 03 PHP
php中smarty模板条件判断用法实例
Jun 11 PHP
标准版Eclipse搭建PHP环境的详细步骤
Nov 18 PHP
PHP正则表达式入门教程(推荐)
May 18 PHP
PHP简单字符串过滤方法示例
Sep 04 PHP
PHP实现文件上传功能实例代码
May 18 PHP
PHP实现的文件上传类与用法详解
Jul 05 PHP
Django 中 cookie的使用
Aug 17 PHP
PHP的mysqli_sqlstate()函数讲解
Jan 23 PHP
C#使用PHP服务端的Web Service通信实例
Apr 08 #PHP
php实现水仙花数的4个示例分享
Apr 08 #PHP
php命令行使用方法和命令行参数说明
Apr 08 #PHP
php实现httpclient类示例
Apr 08 #PHP
php使用json_encode对变量json编码
Apr 07 #PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
Apr 05 #PHP
PHP中的Memcache详解
Apr 05 #PHP
You might like
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
2014/06/23 PHP
php操作XML、读取数据和写入数据的实现代码
2014/08/15 PHP
使用PHP+AJAX让WordPress动态加载文章的教程
2015/12/11 PHP
PHP的Yii框架中YiiBase入口类的扩展写法示例
2016/03/17 PHP
PHP实现支付宝即时到账功能
2016/12/21 PHP
Eclipse PHPEclipse 配置的具体步骤
2017/08/08 PHP
Prototype使用指南之selector.js
2007/01/10 Javascript
FireFox中textNode分片的问题
2007/04/10 Javascript
Javascript 判断函数类型完美解决方案
2009/09/02 Javascript
15款优秀的jQuery导航菜单插件分享
2011/07/19 Javascript
js获得当前时区夏令时发生和终止的时间代码
2014/02/23 Javascript
jquery实现弹出层遮罩效果的简单实例
2014/03/03 Javascript
Javascript实现鼠标框选操作  不是点击选取
2016/04/14 Javascript
简单理解js的冒泡排序
2016/12/19 Javascript
JS ES6多行字符串与连接字符串的表示方法
2017/04/26 Javascript
JavaScript实现的DOM树遍历方法详解【二叉DOM树、多叉DOM树】
2018/05/07 Javascript
教你如何用node连接redis的示例代码
2018/07/12 Javascript
axios简单实现小程序延时loading指示
2018/07/30 Javascript
layui table 参数设置方法
2018/08/14 Javascript
vue插件mescroll.js实现移动端上拉加载和下拉刷新
2019/03/07 Javascript
JavaScript实现美化滑块效果
2019/05/17 Javascript
vue-cli2与vue-cli3在一台电脑共存的实现方法
2019/09/25 Javascript
vue通过v-html指令渲染的富文本无法修改样式的解决方案
2020/05/20 Javascript
React实现阿里云OSS上传文件的示例
2020/08/10 Javascript
[59:08]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第一局
2016/02/27 DOTA
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
2016/08/30 Python
python实现查找所有程序的安装信息
2020/02/18 Python
python 线程的五个状态
2020/09/22 Python
洲际酒店集团美国官网:IHG美国
2017/11/16 全球购物
Ariat官网:美国马靴和服装品牌
2019/12/16 全球购物
小学教师办公室制度
2014/02/03 职场文书
2014年党员干部四风问题自我剖析材料
2014/09/29 职场文书
2015年三万活动总结
2015/03/25 职场文书
2015年项目经理工作总结
2015/04/30 职场文书
2016年社区“6.26”禁毒日宣传活动总结
2016/04/05 职场文书
Python 全局空间和局部空间
2022/04/06 Python