简体   繁体   中英

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

显然,我需要做的就是确保命令末尾包含一个\\n !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM