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 相关文章推荐
建立文件交换功能的脚本(一)
Oct 09 PHP
小偷PHP+Html+缓存
Nov 25 PHP
php array_flip() 删除数组重复元素
Jan 14 PHP
php curl常见错误:SSL错误、bool(false)
Dec 28 PHP
PHP输出当前进程所有变量/常量/模块/函数/类的示例
Nov 07 PHP
thinkphp3查询mssql数据库乱码解决方法分享
Feb 11 PHP
PDO预处理语句PDOStatement对象使用总结
Nov 20 PHP
php绘制一个矩形的方法
Jan 24 PHP
PHP控制反转(IOC)和依赖注入(DI)
Mar 13 PHP
利用ajax和PHP实现简单的流程管理
Mar 23 PHP
tp框架(thinkPHP)实现三次登陆密码错误之后锁定账号功能示例
May 24 PHP
详解PHP Swoole与TCP三次握手
May 27 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
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
教你如何开启shopnc b2b2c 伪静态
2014/10/21 PHP
编写PHP脚本过滤用户上传的图片
2015/07/03 PHP
PHP的中使用非缓冲模式查询数据库的方法
2017/02/05 PHP
javascript使用中为什么10..toString()正常而10.toString()出错呢
2013/01/11 Javascript
JS控制阿拉伯数字转为中文大写示例代码
2013/09/04 Javascript
js调试系列 初识控制台
2014/06/18 Javascript
JS+CSS实现的蓝色table选项卡效果
2015/10/08 Javascript
jQuery点击导航栏选中更换样式的实现代码
2017/01/23 Javascript
JS 组件系列之BootstrapTable的treegrid功能
2017/06/16 Javascript
Vue.js弹出模态框组件开发的示例代码
2017/07/26 Javascript
react路由配置方式详解
2017/08/07 Javascript
如何用webpack4带你实现一个vue的打包的项目
2018/06/20 Javascript
基于Vue实现图片在指定区域内移动的思路详解
2018/11/11 Javascript
JavaScript ES6常用基础知识总结
2019/02/09 Javascript
layui表格数据重载
2019/07/27 Javascript
[46:55]LGD vs Liquid 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
python3实现抓取网页资源的 N 种方法
2017/05/02 Python
python中利用xml.dom模块解析xml的方法教程
2017/05/24 Python
python 自动去除空行的实例
2018/07/24 Python
django框架之cookie/session的使用示例(小结)
2018/10/15 Python
在tensorflow中设置保存checkpoint的最大数量实例
2020/01/21 Python
Python3 获取文件属性的方式(时间、大小等)
2020/03/12 Python
python获取时间戳的实现示例(10位和13位)
2020/09/23 Python
css3 条纹化和透明化表格Firefox下测试成功
2014/04/15 HTML / CSS
HTML5之SVG 2D入门1—SVG(可缩放矢量图形)概述
2013/01/30 HTML / CSS
深入解析HTML5使用SVG图像时的viewBox属性用法
2015/09/02 HTML / CSS
viagogo英国票务平台:演唱会、体育比赛、戏剧门票
2017/03/24 全球购物
商场端午节活动方案
2014/01/29 职场文书
委托公证书
2014/04/08 职场文书
农村文化活动总结
2014/08/28 职场文书
2014入党积极分子批评与自我批评思想报告
2014/10/06 职场文书
基石观后感
2015/06/12 职场文书
python基础之停用词过滤详解
2021/04/21 Python
详解在OpenCV中如何使用图像像素
2022/03/03 Python
在容器中使用nginx搭建上传下载服务器
2022/05/11 Servers