I have a php file inside my apache server that needs to run a shell script (also in the server). When I try the code in putty, it runs the shell script as intended. However, ssh is not able to connect, and the code below gives echos
"Disconnected from server!" "Login Failed".
<?php
$script = "sudo bash /path/script.sh parameter";
set_include_path( __DIR__ .'/phpseclib/' . PATH_SEPARATOR .
get_include_path());
include('Net/SSH2.php');
$ssh = new Net_SSH2($server);
$ssh->enablePTY();
if (!$ssh->login($username, $password)) {
echo $ssh->isConnected() ? 'Connected but Not Logged In' : 'Disconnected From Server!';
exit("\nLogin Failed");
}
$ssh->setTimeout(1);
$ssh->write($script."\n");
echo "Initialized Shell Script\n";
$ssh->read('password for $username:');
echo "Password Confirmation\n";
$ssh->write($password.'\n');
echo "Password Login\n";
echo $ssh->read('[prompt]');
?>
The Net/SSH2 library supports debugging and error reporting. The documentation for the library describes how to debug the functions and view the errors.
You can call the function $ssh->getErrors()
just before the exit("\\nLogin Failed");
function. It will return the errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.