【发布时间】:2011-10-30 02:23:16
【问题描述】:
我正在寻找有关如何使用fsockopen() 与 telnet 系统进行通信的指南……我连接得很好,但是命令发送失败。我看过一些文档 fwrite() 显示人们发送了一些标头。
目前我对 telnet 服务器运行的命令是 version via $class->send("version");。我是否需要发送标头或其他任何内容以供 telnet 服务器接收命令,或者我可以直接发送吗?
/**
* Connect to the GMC telnet system
*/
public function connect () {
$this->connection = fsockopen($this->socket['host'], $this->socket['port'], $errorNumber, $errorMessage, 30);
if (!$this->connection) {
$this->error = 'Unable to connect to GMC: '.$errorMessage.' ('.$errorNumber.')';
return false;
}
stream_set_timeout($this->connection, $this->commandTimeout);
return true;
}
/**
* Send a command to GMC
*/
public function send ($command) {
//write to socket
if (fwrite($this->connection, $command) === false) {
$this->error = 'Unable to write to socket';
return false;
}
sleep(1);
//read socket
if (($response = fgets($this->connection)) === false) {
$this->error = 'Unable to write to socket';
return false;
}
return $response;
}
/**
* Disconnects from the GMC telnet system
*/
public function disconnect () {
return fclose($this->connection);
}
【问题讨论】:
-
一个 telnet 服务器意味着您正在使用远程 shell 帐户...您为此完成了登录过程吗?您不能只连接到 telnet 并开始发出命令 - 在进入 shell 阶段之前,它首先需要一个用户名/密码。
-
我通过本地机器连接到 telnet,它没有要求输入用户名或密码。这是在没有用户连接到的唯一本地服务器上,只有应用程序。
-
即使它只是在您的机器上,也有一个 telnet 端口可以进行登录。您应该发送的第一件事是用户名/密码,带有
\n表示您已完成输入。