I have PHP script which uses ssh utility to connect to a remote Ubuntu machine for executing some commands there. The script is as given below.
<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('10.3.2.0');
if (!$ssh->login('makesubdomain','abcdabcd')) {
exit('Login Failed');
}
echo $ssh->exec('/usr/local/listdomain.backup/test/makedir.sh');
?>
Here my problem is, how can I give an if
condition to check whether the connection failed or not. I am trying to modify the above code something like this, but I know this is very wrong.
<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
if(!$ssh = new Net_SSH2('10.3.2.0')) // Here how can I give condtion
{
echo "Connection failed";
}else{
if (!$ssh->login('abcd','abcdabcd')) {
exit('Login Failed');
}
echo $ssh->exec('./test.sh');
}
?>
Please help me as I am very beginner in php. Thanks
The answer you're looking for is:
It's already all there in the example you've given, no need to modify the code.
$ssh->login()
does exactly what you want: It tries to connect AND login to the server you've given it, and it will return false in case that failed.