1

ソケット (ポート 23 の telnet) との間で文字列を読み書きする「単純な」PHP プログラムを作成しようとしています。

ソケット接続は pfsockopen (または socket_connect) 関数を使用して行われ、正しく動作しているようです (wireshark で検証済み)。問題は、fread または socket_read を使用してソケットから読み取ろうとしたときです。これら 2 つの関数によって返されるバッファは読み取り不能です (テキストではなく特殊な ASCII 文字)。

php スクリプトが Windows XP PC で実行されているとします。これらのライブラリを Windows PC および telnet プロトコルで使用する際の制限について知っていますか?

    $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("10.129.127.33",23);
fputs($fp,$header1);
sleep(1);
$output=fread($fp,8164); 
echo $output;
4

2 に答える 2

1
  1. 接続は成功していますか?
  2. サーバーはポート 23 での接続を許可していますか?
  3. これはあなたを助けるかもしれません

fputs($fp,"文字列");

do 
{ 
$output.=fread($fp, 80); 
// read line by line, or at least small chunks
$stat=socket_get_status($fp);
}
while($stat["unread_bytes"]);

$output = str_replace("\n", "<br>", $output);\\for new line in HTML
echo $output;
  1. 以下は、私が使用したTELNETヘッダーです

    $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).ch.(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);
            $header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
    
    $fp=fsockopen("192.168.125.6",23);  
    
    
    //**********Send telnet header****************
    
    fputs($fp,$header1);
    
    usleep(1000);
    
    fputs($fp,$header2);
    
    usleep(1000);
    
于 2015-11-14T19:49:52.327 に答える
0
<?php
# Generating output by telneting a router

$username='myusername';
$password='mypassword';
$ip="192.168.0.1";

    $Telnet_Header = 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);
$Telnet_Header2 = chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
$Port = 23;
$Conn = fsockopen($ip, $Port);
fputs($Conn,$Telnet_Header);
sleep(1);
fputs($Conn,$Telnet_Header2);
sleep(1);
fputs($Conn,$username."\r");
sleep(5);
fputs($Conn,$password."\r");
sleep(1);
fputs($Conn,$username."\r");
sleep(5);
fputs($Conn,$password."\r");
sleep(5);
fputs($Conn,"en \r"); 
   sleep(3);
fputs($Conn,"conf t\r"); 
   sleep(3);
fputs($Conn,"terminal length 0\r"); 
   sleep(3);
fputs($Conn,"sh run\r"); 

   $output="";
do
{
    $output.=fread($Conn, 1000);
    $stat=socket_get_status($Conn);
}while($stat["unread_bytes"]);
$output = explode("\n", $output);
for ($i=6;$i<count($output)-1;$i++)
    {
    echo ($output[$i]."\n");
    }

?>
于 2017-12-04T19:10:11.093 に答える