What is the purpose of using ssh2_exec in PHP and what are some common use cases?

The purpose of using ssh2_exec in PHP is to execute a command on a remote server via SSH protocol. This function allows you to run commands on a remote server securely and retrieve the output. Common use cases include automating tasks on remote servers, fetching information from a remote server, or managing server configurations.

// Connect to remote server via SSH
$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

// Execute command on remote server
$stream = ssh2_exec($connection, 'ls -la');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);

// Display output
echo $output;