PHP Telnet程序代码

网络 网络管理
文章摘要:下面我们来对PHP Telnet程序的具体代码详细讲解一下。那么文中所设计的内容希望能够给大家提供一个参考作用。

Telnet可以帮助我们完成远程登录。其中实现它的代码形式并不固定,这里我们介绍的则是PHP Telnet程序。那么就让我们来一起看看具体的代码是如何编写的。那么具体的内容请大家浏览正文。

PHP Telnet程序

PHP代码:

  1. PHP code  
  2.  <?php 
  3.  /**  
  4. * name:薛如飞  
  5. * qq:6706250  
  6. * e-mail:xuerufei@163.com  
  7. * blog:[url=http://hi.baidu.com/]http://hi.baidu.com/[/url]飞云盖天  
  8. * date:08.08.28  
  9. **/  
  10. if (isset($_POST['host']) and isset($_POST['user']))  
  11. {  
  12.  $host= $_POST['host'];  
  13.  $user= $_POST['user'];  
  14.  $pass= $_POST['pass'];  
  15.  $cmdstripslashes($_POST['cmd']);  
  16.  require_once "phpTelnet.php";  
  17.  $Telnet = new PHPTelnet();  
  18.  $Telnet->show_connect_error=0;  
  19.  $result = $Telnet->Connect($host,$user,$pass);  
  20.  switch ($result)  
  21.  {  
  22.   case 0:  
  23.    $Telnet->DoCommand($cmd, $result);  
  24.   echo $result;  
  25.   $Telnet->Disconnect();  
  26.   break;  
  27.   case 1:  
  28.    echo '[PHP Telnet] Connect failed: Unable to open network connection';  
  29.   break;  
  30.   case 2:  
  31.    echo '[PHP Telnet] Connect failed: Unknown host';  
  32.   break;  
  33.   case 3:  
  34.    echo '[PHP Telnet] Connect failed: Login failed';  
  35.   break;  
  36.   case 4:  
  37.    echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';  
  38.   break;  
  39.  }  
  40. }  
  41. else  
  42. {  
  43.  ?> 
  44.   <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  45.   <form action="index.php" method="post" name="form0" id="form0"> 
  46.   <p></p> 
  47.   <p align="center" >Telnet</p> 
  48.   <table width="200" border="0" align="center"> 
  49.   <tr> 
  50.   <td width="81" height="18">host:</td> 
  51.   <td width="109"><input name="host" type="text" size="16" value="" /></td> 
  52.   </tr> 
  53.   <tr> 
  54.   <td width="81" height="18">user:</td> 
  55.   <td width="109"><input name="user" type="text" size="16" value="" /></td> 
  56.   </tr> 
  57.   <tr> 
  58.   <td width="81" height="18">pass:</td> 
  59.   <td width="109"><input name="pass" type="text" size="16" value="" /></td> 
  60.   </tr> 
  61.   <tr> 
  62.   <td width="81" height="18">cmd:</td> 
  63.   <td width="109"> 
  64.   <textarea rows="6" name="cmd" cols="16"></textarea></td> 
  65.   </tr> 
  66.   <tr> 
  67.   <td> </td> 
  68.   <td><input type="submit" name="Submit" value="提交" /></td> 
  69.   </tr> 
  70.   </table> 
  71.   <p> </p> 
  72.   </form> 
  73.   <?php 
  74.  }  
  75. ?> 
  76.  
  77.  <?php 
  78.  /*  
  79. PHP Telnet 1.1  
  80. by Antone Roundy  
  81. adapted from code found on the PHP website  
  82. public domain  
  83. */  
  84. class PHP Telnet   
  85. {  
  86.  var $show_connect_error=1;  
  87.  var $use_usleep=0; // change to 1 for faster execution  
  88.  // don't change to 1 on Windows servers unless you have PHP 5  
  89.  var $sleeptime=125000;  
  90.  var $loginsleeptime=1000000;  
  91.  var $fp=NULL;  
  92.  var $loginprompt;  
  93.  
  94.  var $conn1;  
  95.  var $conn2;  
  96.  
  97.  /*  
  98. 0 = success 
  99. 1 = couldn't open network connection  
  100. 2 = unknown host  
  101. 3 = login failed  
  102. 4 = PHP version too low  
  103. */  
  104.  function Connect($server,$user,$pass)   
  105.  {  
  106.   $rv=0;  
  107.   $vers=explode('.',PHP_VERSION);  
  108.   $needvers=array(4,3,0);  
  109.   $j=count($vers);  
  110.   $k=count($needvers);  
  111.   if ($k<$j) $j=$k;  
  112.   for ($i=0;$i<$j;$i++)   
  113.   {  
  114.    if (($vers[$i]+0)>$needvers[$i]) break;  
  115.    if (($vers[$i]+0)<$needvers[$i])   
  116.    {  
  117.     $this->ConnectError(4);  
  118.     return 4;  
  119.    }  
  120.   }  
  121.  
  122.   $this->Disconnect();  
  123.  
  124.   if (strlen($server))   
  125.   {  
  126.    if (preg_match('/[^0-9.]/',$server))   
  127.    {  
  128.     $ip=gethostbyname($server);  
  129.     if ($ip==$server)   
  130.     {  
  131.      $ip='';  
  132.      $rv=2;  
  133.     }  
  134.    }  
  135.    else $ip=$server;  
  136.   }  
  137.   else $ip='127.0.0.1';  
  138.  
  139.   if (strlen($ip))   
  140.   {  
  141.    if ($this->fp=fsockopen($ip,23))   
  142.    {  
  143.     fputs($this->fp,$this->conn1);  
  144.     $this->Sleep();  
  145.  
  146.     fputs($this->fp,$this->conn2);  
  147.     $this->Sleep();  
  148.     $this->GetResponse($r);  
  149.     $r=explode("\n",$r);  
  150.     $this->loginprompt=$r[count($r)-1];  
  151.     fputs($this->fp,"$user\r");  
  152.     $this->Sleep();  
  153.     fputs($this->fp,"$pass\r");  
  154.     if ($this->use_usleep) usleep($this->loginsleeptime);  
  155.     else sleep(1);  
  156.     $this->GetResponse($r);  
  157.     $r=explode("\n",$r);  
  158.     if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1]))   
  159.     {  
  160.      $rv=3;  
  161.      $this->Disconnect();  
  162.     }  
  163.    }  
  164.    else $rv=1;  
  165.   }  
  166.  
  167.   if ($rv) $this->ConnectError($rv);  
  168.   return $rv;  
  169.  }  
  170.  
  171.  function Disconnect($exit=1)   
  172.  {  
  173.   if ($this->fp)   
  174.   {  
  175.    if ($exit) $this->DoCommand('exit',$junk);  
  176.    fclose($this->fp);  
  177.    $this->fp=NULL;  
  178.   }  
  179.  }  
  180.  function DoCommand($c,&$r)   
  181.  {  
  182.   if ($this->fp)   
  183.   {  
  184.    fputs($this->fp,"$c\r");  
  185.    $this->Sleep();  
  186.    $this->GetResponse($r);  
  187.    $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);  
  188.   }  
  189.   return $this->fp?1:0;  
  190.  }  
  191.  
  192.  function GetResponse(&$r)   
  193.  {  
  194.   $r='';  
  195.   do   
  196.   {  
  197.    $r.=fread($this->fp,1000);  
  198.    $s=socket_get_status($this->fp);  
  199.   }  
  200.   while ($s['unread_bytes']);  
  201.  }  
  202.  
  203.  function Sleep()   
  204.  {  
  205.   if ($this->use_usleep) usleep($this->sleeptime);  
  206.   else sleep(1);  
  207.  }  
  208.  
  209.  function PHP Telnet()   
  210.  {  
  211.   $this->conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).  
  212.    chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).  
  213.    chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).  
  214.    chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).  
  215.    chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).  
  216.    chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).  
  217.    chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).  
  218.    chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).  
  219.    chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).  
  220.    chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).  
  221.    chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).  
  222.    chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);  
  223.   $this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).  
  224.    chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);  
  225.  }  
  226.  
  227.  function ConnectError($num)   
  228.  {  
  229.   if ($this->show_connect_error) switch ($num)   
  230.   {  
  231.    case 1: echo '<br />[PHP Telnet] <a href="[url=http://www.geckotribe.com/php-Telnet/errors/fsockopen.php]http://www.geckotribe.com/php-Telnet/errors/fsockopen.php">Connect[/url] failed: Unable to open network connection</a><br />'; break;  
  232.     case 2: echo '<br />[PHP Telnet] <a href="[url=http://www.geckotribe.com/php-Telnet/errors/unknown-host.php]http://www.geckotribe.com/php-Telnet/errors/unknown-host.php">Connect[/url] failed: Unknown host</a><br />'; break;  
  233.     case 3: echo '<br />[PHP Telnet] <a href="[url=http://www.geckotribe.com/php-Telnet/errors/login.php]http://www.geckotribe.com/php-Telnet/errors/login.php">Connect[/url] failed: Login failed</a><br />'; break;  
  234.     case 4: echo '<br />[PHP Telnet] <a href="[url=http://www.geckotribe.com/php-Telnet/errors/php-version.php]http://www.geckotribe.com/php-Telnet/errors/php-version.php">Connect[/url] failed: Your server\'s PHP version is too low for PHP Telnet</a><br />'; break;  
  235.    }  
  236.  }  
  237. }  
  238. ?> 

 

责任编辑:佟健 来源: IT168
相关推荐

2011-11-09 13:59:27

代码腐烂

2013-07-29 14:28:43

JQueryJQuery实现分页分页程序代码

2011-11-03 15:44:10

程序员

2010-07-13 09:29:37

socketUDP协议

2009-06-03 14:42:21

Eclipse调试调试Java程序

2010-01-22 15:09:11

VB.NET下载程序

2009-06-17 14:29:50

java程序代码

2009-08-24 18:06:36

源程序代码C#读取XML文件

2010-01-15 10:48:29

C++程序代码

2010-03-23 14:12:43

Python开发Win

2014-01-16 13:36:17

2009-09-02 18:28:00

C#鼠标位置

2010-01-15 18:46:08

C++程序代码

2010-05-28 10:53:07

Linux串口测试工具

2013-04-22 11:34:30

BadNews恶意程序移动安全

2023-11-17 11:55:54

Pythonretrying库

2010-07-16 14:22:25

Python teln

2009-10-27 10:14:54

VB.NET正则表达式

2021-05-07 09:00:00

JavaScript开发代码

2010-08-06 09:33:08

DB2 JDBC连接
点赞
收藏

51CTO技术栈公众号