2012-11-15 89 views
2

我試圖構建一個php腳本,它連接到telnet服務器並運行一些命令。當使用fsockopen時將php輸出保存到變量

我想將命令的返回輸出保存到變量中。

例如: 命令:給我一個數字! 服務器返回一個數字..可以說它是100. 我想將數字100保存爲變量。

這裏是我的源代碼:

<?php 
# connecting 
$fp=fsockopen("10.73.xxx.xxx",23); 

# login 
fputs($fp,"\r"); 
sleep(1); 
fputs($fp,"user\r"); 
sleep(1); 
fputs($fp,"password\r"); 

# commands 
fputs($fp,"give me a number!\n"); //this returns the number I would like to save as variable 

sleep(1); 

fclose($fp); 
?> 

回答

1

你可能想stream_get_line

$theData = stream_get_line($fp, 1024, "\n"); 
// 1024 = The maximum number of bytes to read from the handle. 
// \n = string delimiter.