PHP function ssh2_connect is not working
The error Fatal error: Call to undefined function ssh2_connect() indicates that your PHP interpreter cannot find the implementation for the SSH extension. This is not a logic error within your script; it is an environment configuration issue. The functions ssh2_connect, ssh2_auth_password, and ssh2_exec are part of the PECL extension named ssh2. Unlike standard PHP core functions that come pre-installed with most distributions, this extension must be explicitly compiled or installed on your server.
When you see this error, the most immediate step is to verify whether the SSH2 extension is loaded in your current environment. You can check this by running php -m from your terminal. If you do not see ssh2 listed in the output of that command, the extension is either not installed or not enabled for the specific PHP binary your web server is using.
Enabling the Extension
If the extension exists on the system but isn't loaded, you need to enable it via configuration. In many shared hosting environments or standard Linux distributions like Ubuntu or CentOS, this is done by editing the php.ini file. Look for a line that reads ;extension=ssh2. Remove the semicolon to uncomment it:
extension=ssh2
After making this change, you must restart your web server (Apache or Nginx) or clear your opcache for the changes to take effect. If you are installing PHP from source on a system without PECL support pre-configured, you may need to install the development package first:
# On Debian/Ubuntu
sudo apt-get install php-ssh2
# On CentOS/RHEL
sudo yum install php-pecl-ssh2
Once installed, verify again with php -m. If the module is still missing after installation and configuration, ensure you are editing the php.ini file corresponding to the PHP version your web server is actually running (e.g., php7.4.ini vs php8.0.ini).
Correcting the Script Logic
Assuming the extension is now loaded, your script contains a critical logical error that will prevent it from functioning even after fixing the extension issue. You are attempting to change a user's password using ssh2_exec, but the syntax you are using is incorrect. The function ssh2_exec sends a command string and expects a stream resource back; it does not automatically send input to the SSH process unless you explicitly interact with that stream.
The commands useradd -d /home/users/test -m testftp and passwd testftp require interactive input. Specifically, the passwd command will prompt for the new password twice. Your current code creates the stream but then immediately tries to assign it to a new variable $stream without actually sending the password data over that stream.
To execute these commands correctly, you must open a stream from the result of ssh2_exec and write to it using ssh2_send_string. Here is the corrected approach:
<?php
// Verify extension is loaded first
if (!function_exists('ssh2_connect')) {
throw new RuntimeException("SSH2 extension not loaded.");
}
$connection = ssh2_connect('XX.XX.XX.XX', 22);
ssh2_auth_password($connection, 'root', '******');
// Create the user
$user_stream = ssh2_exec($connection, 'useradd -d /home/users/test -m testftp');
if ($user_stream) {
// Change the password. Note: passwd requires input on stdin.
$passwd_stream = ssh2_exec($connection, 'passwd testftp');
if ($passwd_stream) {
// Send the new password twice as prompted by the system
ssh2_send_string($passwd_stream, "password\n");
ssh2_send_string($passwd_stream, "password\n");
// Flush the buffer to ensure input is sent immediately
stream_set_blocking($passwd_stream, false);
stream_select(array($passwd_stream), null, null, 1);
// Close streams and connection
ssh2_fclose($passwd_stream);
}
ssh2_fclose($user_stream);
}
ssh2_close($connection);
?>
Security Considerations
While using ssh2_exec for administrative tasks is convenient, it should be treated with caution. The ssh2_connect function establishes a direct channel to the remote host. If you are automating sensitive operations like adding users, ensure that your script runs on a secure server and that the credentials used are not exposed in logs or source control.
Additionally, relying on interactive commands via ssh2_exec can be brittle if the remote shell behaves differently than expected (e.g., different prompts or error messages). A more robust architecture often involves using SSH key-based authentication combined with non-interactive scripts executed via sudo -u username bash -c 'command', which avoids password prompts entirely.
For complex enterprise needs, consider whether a dedicated orchestration tool is better suited than raw PHP SSH commands. If you are looking for managed cloud resources where SSH management is handled abstractly, platforms like https://laravelcompany.com offer infrastructure solutions that reduce the need for manual ssh2_connect implementations in your application code.
