What potential security risks are associated with using SSH in PHP scripts?

Using SSH in PHP scripts can pose security risks such as exposing sensitive information like passwords in the code, potential for remote code execution if not properly sanitized, and the possibility of unauthorized access to the server if credentials are compromised. To mitigate these risks, it is important to store SSH credentials securely and avoid hardcoding them in the script.

<?php
// Store SSH credentials securely
$ssh_host = 'example.com';
$ssh_username = 'username';
$ssh_password = 'password';

// Connect to SSH server
$connection = ssh2_connect($ssh_host);

if ($connection) {
    // Authenticate with SSH server
    if (ssh2_auth_password($connection, $ssh_username, $ssh_password)) {
        // Execute commands on SSH server
        $stream = ssh2_exec($connection, 'ls -la');
        stream_set_blocking($stream, true);
        $output = stream_get_contents($stream);
        echo $output;
        fclose($stream);
    } else {
        echo 'Authentication failed.';
    }

    // Close SSH connection
    ssh2_disconnect($connection);
} else {
    echo 'Connection failed.';
}
?>