简体   繁体   English

从php访问ec2的ssh

[英]ssh access to ec2 from php

From local ubuntu linux machine connecting ec2 machine by ssh access. 从本地ubuntu linux机器通过ssh访问连接ec2机器。

when i run this php script from terminal it executes fine and write the tailed entries in file. 当我从终端运行这个PHP脚本时,它执行正常并在文件中写入尾部条目。 when i run from browser got this error in apache error log 当我从浏览器运行时在apache错误日志中得到此错误

ssh: Could not resolve hostname proxy2: Name or service not known ssh:无法解析主机名proxy2:名称或服务未知

Found its due to apache user permission problem. 发现它由于apache用户权限问题。 whether my guess is right or wrong am not sure. 我的猜测是对还是错,我不确定。 Any one helps me to fix this issue. 任何人都可以帮我解决这个问题。

php code:- php代码: -

<?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. **终于找到了使用phpseclib的解决方案并解决了我的问题。 am recommend phpseclib to connect amazon ec2 machines from php uing .pem file to help others share my sample code. 我建议phpseclib从php uing .pem文件连接amazon ec2机器,以帮助其他人分享我的示例代码。

make sure .pem file needs permission to read** 确保.pem文件需要读取权限**

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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