ssh access to ec2 from php

2019-06-12 01:12发布

From local ubuntu linux machine connecting ec2 machine by ssh access.

when i run this php script from terminal it executes fine and write the tailed entries in file. when i run from browser got this error in apache error log

ssh: Could not resolve hostname proxy2: Name or service not known

Found its due to apache user permission problem. whether my guess is right or wrong am not sure. Any one helps me to fix this issue.

php code:-

<?php

$ss = 'ssh proxy2 '.'tail -n 3 /out/speed_log.txt.1'.' > proxy2temp1';
system($ss);
?>

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-12 01:26

**Finally found an solution using phpseclib and solved my problem. am recommend phpseclib to connect amazon ec2 machines from php uing .pem file to help others share my sample code.

make sure .pem file needs permission to read**

sample code:

include('Net/SSH2.php');
include('Crypt/RSA.php');

$key = new Crypt_RSA();

$key->loadKey(file_get_contents('/pathtokey.pem'));


$ssh = new Net_SSH2('ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com');
if (!$ssh->login('user', $key)) {
exit('Login Failed');
}

echo $ssh->exec('tail -n 3 /out/_log.txt.1');
查看更多
登录 后发表回答