php实现telnet功能示例
2015-01-24信息快讯网
这篇文章主要介绍了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="http://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="http://www.jb51.net/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a><br />'; break; case 3: echo '<br />[PHP Telnet] <a href="http://www.jb51.net/php-telnet/errors/login.php">Connect failed: Login failed</a><br />'; break; case 4: echo '<br />[PHP Telnet] <a href="http://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在模型中自动完成session赋值小教程
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
php设计模式之简单工厂模式详解
深入分析PHP引用(&)
Yii 快速,安全,专业的PHP框架
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
ThinkPHP后台首页index使用frameset时的注意事项分析
PHP中new static()与new self()的区别异同分析
asp.net访问网络路径方法(模拟用户登录)
php socket实现的聊天室代码分享
php与flash as3 socket通信传送文件实现代码
getJSON跨域SyntaxError问题分析
VB中的RasEnumConnections函数返回632错误解决方法
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
C#使用PHP服务端的Web Service通信实例
php实现水仙花数的4个示例分享
php命令行使用方法和命令行参数说明
php实现httpclient类示例
php使用json_encode对变量json编码
PHP fopen()和 file_get_contents()应用与差异介绍
php过滤所有恶意字符(批量过滤post,get敏感数据)
php使用websocket示例详解
PHP中isset()和unset()函数的用法小结
php function用法如何递归及return和echo区别
分享下页面关键字抓取components.arrow.com站点代码
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
PHP使用SOAP调用.net的WebService数据
php define的第二个参数使用方法
简单实用的.net DataTable导出Execl