代码之家  ›  专栏  ›  技术社区  ›  farzad

使用PHP的telnet连接

  •  10
  • farzad  · 技术社区  · 15 年前

    更新: 这个要求(显而易见)是很久以前的事了。最后,我不得不编写自己需要的客户端库。此库(以及更多模块)的代码是开源的,可在 github 谢谢大家的回答。

    4 回复  |  直到 11 年前
        1
  •  6
  •   Soleil    11 年前

    Nicholas Hall的Github上有一个可爱的PHP telnet连接类: https://github.com/ngharo/Random-PHP-Classes/blob/master/Telnet.class.php

        2
  •  5
  •   131    11 年前

    使用标准输入/流_选择&阻塞流为您提供了一个类似于telnet的20行客户端

    <?
    
    $socket = fsockopen("192.168.52.1", 8000);
    
    if(!$socket)return;
    stream_set_blocking($socket, 0);
    stream_set_blocking(STDIN, 0);
    
    do {
      echo "$ ";
      $read   = array( $socket, STDIN); $write  = NULL; $except = NULL;
    
      if(!is_resource($socket)) return;
      $num_changed_streams = @stream_select($read, $write, $except, null);
      if(feof($socket)) return ;
    
    
      if($num_changed_streams  === 0) continue;
      if (false === $num_changed_streams) {
          /* Error handling */
        var_dump($read);
        echo "Continue\n";
        die;
      } elseif ($num_changed_streams > 0) {
        echo "\r";
        $data = fread($socket, 4096);
        if($data !== "") 
          echo "<<< $data";
    
        $data2 = fread(STDIN, 4096);
    
        if($data2 !== "") {
          echo ">>> $data2";
          fwrite($socket, trim($data2));
        }
      }
    
    } while(true);
    
        3
  •  4
  •   Dorkfest    11 年前

    Pear::网络插座: http://pear.php.net/package/Net_Socket 为简单的PHP telnet bot或会话扩展此类。

        4
  •  3
  •   Earlz    14 年前
    <?php
    
    $file = 'somefile.txt';
    $remote_file = 'readme.txt';
    // set up basic connection
    $ftp_server = '127.0.0.1';
    $ftp_user_name = 'Till';
    $ftp_user_pass = 'Kcp05';
    $conn_id = ftp_connect($ftp_server);
    // login with username and password
    ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    // upload a file
    ftp_nb_put($conn_id, $remote_file, $file, FTP_ASCII);
    // close the connection
    echo "$file sent to $ftp_server as $remote_file\n<br/>";
    ftp_close($conn_id);
    
    
    // finished copying the input.dat to the till now, just need to execute the print command.
    // That will copy somefile.txt in the same folder as this .php file to the ftp server root dir.
    
    
    $header1=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);
    
    $fp=pfsockopen("127.0.0.1",23);
    
    echo "Telnet session opening ...";
    
    sleep(4);
    
    fputs($fp,$header1); 
    sleep(4); 
    
    fputs($fp,"Till\r");
    sleep(2); 
    fputs($fp,"Kcp05\r"); 
    
    sleep(2);
    fputs($fp,"notepad\r"); 
    
    sleep(3);
    
    echo "Telnet session closing ...";
    
    fclose($fp);
    
    ?> 
    

    这对我很管用。第一部分将ftp文件上载到服务器,第二部分将lo上载到telnet服务器,并执行一个程序,该程序可以使用您刚才通过ftp上载的文件。刚刚测试过。