本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑
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; } } } ?>
推荐学习:《PHP视频教程》
以上就是php怎么实现telnet的详细内容,更多请关注亿码酷站其它相关文章!
<!––>php怎么实现telnet
—–文章转载自PHP中文网如有侵权请联系ymkuzhan@126.com删除
转载请注明来源:php怎么实现telnet
本文永久链接地址:https://www.ymkuzhan.com/46893.html
本文永久链接地址:https://www.ymkuzhan.com/46893.html
下载声明:
本站资源如无特殊说明默认解压密码为www.ymkuzhan.com建议使用WinRAR解压; 本站资源来源于用户分享、互换、购买以及网络收集等渠道,本站不提供任何技术服务及有偿服务,资源仅提供给大家学习研究请勿作它用。 赞助本站仅为维持服务器日常运行并非购买程序及源码费用因此不提供任何技术支持,如果你喜欢该程序,请购买正版! 版权声明:
下载本站资源学习研究的默认同意本站【版权声明】若本站提供的资源侵犯到你的权益,请提交版权证明文件至邮箱ymkuzhan#126.com(将#替换为@)站长将会在三个工作日内为您删除。 免责声明:
您好,本站所有资源(包括但不限于:源码、素材、工具、字体、图像、模板等)均为用户分享、互换、购买以及网络收集而来,并未取得原始权利人授权,因此禁止一切商用行为,仅可用于个人研究学习使用。请务必于下载后24小时内彻底删除,一切因下载人使用所引起的法律相关责任,包括但不限于:侵权,索赔,法律责任,刑事责任等相关责任,全部由下载人/使用人,全部承担。以上说明,一经发布视为您已全部阅读,理解、同意以上内容,如对以上内容持有异议,请勿下载,谢谢配合!支持正版,人人有责,如不慎对您的合法权益构成侵犯,请联系我们对相应内容进行删除,谢谢!