I have php script in which I used expect for automating login and executing command in a remote machine. This is my php code looks like,
<?php
ini_set("expect.loguser", "Off");
$stream = fopen("expect://ssh [email protected]", "r");
$cases = array (
array (0 => "*(yes/no)?", 1 => YESNO),
array (0 => "*d: ", 1 => PASSWORD),
array (0 => "*$ ", 1 => SHELL, EXP_EXACT)
);
switch (expect_expectl ($stream, $cases)) {
case YESNO:
fwrite ($stream, "yes\n");
break;
case PASSWORD:
fwrite ($stream, "myrootpassword\n");
break;
case SHELL:
fwrite ($stream, "top -n 1\n");
break;
default:
die ("Error was occurred while connecting to the remote host!\n");
}
while ($line = fgets($stream)) {
print $line;
}
fclose ($stream);
?>
and I know I have installed expect module for php also.
I have installed expect in my Ubuntu machine also.
But I don't know why this is script is not working. I followed http://www.php.net/manual/en/book.expect.php and http://php.net/manual/en/expect.examples-usage.php for creating my php script file. Please advice me, as I am a beginner in PHP, what modification shall I need to do for working expect in php.
Thanks.
First of all, most servers are configured by default to NOT allow remote root login via ssh.
Secondly, why not use SSH keyfiles to allow passwordless logins? It is WAY more secure than storing your password.
sudoers
)Instead of using expect://
to run the command, simply run the command directly:
ssh [email protected] sudo theCommand