What are the potential drawbacks of using telnet for executing commands in PHP?

Using telnet for executing commands in PHP can pose security risks as it can allow unauthorized access to the server and expose sensitive information. It is also not a recommended method for executing commands as it lacks encryption and authentication mechanisms. To mitigate these risks, it is recommended to use more secure methods such as SSH for executing commands in PHP.

// Use SSH2 extension for secure command execution
$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ls -la');
stream_set_blocking($stream, true);
$data = '';
while ($buffer = fread($stream, 4096)) {
    $data .= $buffer;
}
fclose($stream);
echo $data;