telnet connection using PHP

telnet connection using PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Telnet Connections in PHP Without External Libraries

Attempting to establish a raw Telnet connection in PHP often leads developers down a rabbit hole of searching for pre-built libraries. The reality is that standard PHP does not include a native Telnet client class, and third-party packages on packagist are frequently abandoned or lack active maintenance. This forces engineers into a position where they must either rely on unverified external dependencies or build the socket logic themselves from scratch.

When I needed to interact with a router remotely—sending commands and parsing the resulting output—I found myself in exactly this situation. The standard approach using proc_open is fragile because it requires managing input/output pipes manually, which complicates error handling. A dedicated TCP socket implementation offers far more control over the connection lifecycle, timeout management, and stream buffering.

The most robust solution involves writing a custom library that wraps the PHP stream wrapper. By encapsulating the raw sockets logic into a reusable class, you gain a clean API for interacting with network devices without bloating your application with low-level complexity. The core of this implementation relies on creating a Telnet class that handles the handshake and data transmission.

Here is how the connection logic works under the hood. You instantiate the client by specifying the target host and port, then set the timeout to prevent hanging indefinitely if the router doesn't respond.

$telnet = new Network\Telnet('192.168.1.1', 23);
$telnet->setTimeout(30); // Set a reasonable timeout in seconds

Once the object is instantiated, you can establish the connection directly. The critical part of this implementation is how it handles the interaction flow. A naive script might simply write a string and expect an immediate response. However, routers often require specific sequences or acknowledge characters before responding to commands. The custom library handles these nuances by allowing you to flush the buffer after sending a command, ensuring the router processes the input before you attempt to read the output.

$telnet->connect();
$telnet->send('enable\r\n'); // Send enable command with line break
$telnet->flush();           // Ensure data is pushed to the socket
$output = $telnet->read(500); // Read up to 500 bytes of response

The trade-off here is that you are responsible for managing the state of the connection. If the router resets or the network drops, the stream will throw an exception, which your application must catch and handle gracefully. Relying on sleep loops to poll the connection is inefficient; instead, the socket's internal timeout mechanism should dictate when the script gives up waiting for data.

Another common pitfall is handling line endings. Telnet protocols strictly rely on \r\n. If you send a command with only a newline character or no line break at all, the router might buffer the input and refuse to execute it until it sees a full line terminator. The custom implementation abstracts this away, but understanding that the protocol expects CRLF is essential for debugging connection issues.

For projects requiring rapid development, you might be tempted to use fsockopen directly. While valid, this approach requires you to manually parse the output stream and handle the underlying socket resource. By adopting a structured class like the one available on GitHub, you offload the boilerplate code involving stream selection and error checking. This allows you to focus on the logic of parsing the router's CLI output rather than wrestling with PHP's stream API.

If you find yourself needing similar network utilities, such as SSH or other protocol handlers, maintaining a consistent interface across different connection types is beneficial. The architecture used for Telnet can often be adapted for other protocols by swapping the underlying transport layer while keeping the command parsing logic identical. This modularity reduces technical debt and makes it easier to onboard new developers who need to interact with network infrastructure using PHP.

Our network