PHP - Authenticate to an SSH server using ssh2_auth_password

by
Jeremy Canfield |
Updated: January 31 2022
| PHP articles
The ssh2_auth_password function can be used to authenticate to an SSH server from PHP with a username and password. Before using the ssh2_auth_password function, you will need to establish a connection to the SSH server using the ssh2_connection function.
In this example, username "john.doe" and password "itsasecret" are used to authenticate to the SSH server.
<?php
$server = "server1.example.com";
$connection = ssh2_connect($server, 22);
if (!$connection) {
echo "Failed to connect to SSH server $server <br />";
}
else {
echo "Successfully connect to SSH server $server <br />";
}
$username = "john.doe";
$password = "itsasecret";
$authenticate = ssh2_auth_password($connection, $username, $password);
if (!$authenticate) {
echo "Failed to authenticate to SSH server $server as $username <br />";
}
else {
echo "Successfully authenticated to SSH server $server as $username <br /";
}
?>
Did you find this article helpful?
If so, consider buying me a coffee over at