Webnet Webnet - 4 months ago 50
PHP Question

PHp fsockopen() communicating via Telnet

I'm looking for a guide on how to use fsockopen() to communicate with a telnet system.... I'm connected just fine, but the command is failing to send. I've seen some documentation

fwrite()
that shows people sending some headers.

Currently the command I'm running against the telnet server is
version
via
$class->send("version");
. Do I need to send headers or anything along with this for the telnet server to pick up the command, or can I just send that?

/**
* 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);
}

Answer

Apparently all I needed to do was be sure I included a \n at the end of my command!!