简体   繁体   中英

ssh access to ec2 from php

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);
?>

**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');

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM