Reputation: 1703
I use the below bash script to connect to a remote private machine using a pass-wordless ssh connect. I then run a set of commands on that machine.
I am trying to do exactly the same thing but run this on a web page using PHP. I am not sure were do i start from
I did try something in php but failed at it miserably not sure how do i do a password less ssh connection in php ?
ssh -t -o "StrictHostKeyChecking no" -i ~/.ssh/iduser_rsa [email protected] '
echo "hostname" > /tmp/iamhere.txt
cd /opt/im/olywc/bin/
echo "Checking server status:"
sudo ./status.sh
echo ""
ps -ef |grep -i main |grep -v grep
echo ""
echo "End of restart function"
exit
'
Converted the above to PHP code
include('Net/SSH2.php');
$ssh = new Net_SSH2('10.xxx.22.133');
if (!$ssh->login('', '')) {
exit('Login Failed');
}
function printOutput($str)
{
echo $str;
}
$ssh->exec('
echo "I am here" > /tmp/iamhere.txt
cd /opt/im/olywc/bin/
echo "Checking server status:"
sudo ./status.sh
echo ""
ps -ef |grep -i main |grep -v grep
echo ""
echo "End of restart function"
exit
'
, 'printOutput');
?>
Upvotes: 1
Views: 889
Reputation: 16802
Looks like you're using RSA key auth in your CLI version of the command. eg. ~/.ssh/iduser_rsa
. So try this:
include('Net/SSH2.php');
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('/home/user/.ssh/idusa_rsa'));
$ssh = new Net_SSH2('10.xxx.22.133');
if (!$ssh->login('', $rsa)) {
exit('Login Failed');
}
Upvotes: 0
Reputation: 6319
phpseclib (http://phpseclib.sourceforge.net/) is a well maintained library for this. I've had a little experience using this library for key generation but it will allow you to create a simple SSH connection using any of the usual protocols and run commands through an SSH connection.
Check this section of the documentation.
Upvotes: 1