ssh access to ec2 from php



从本地ubuntu linux机通过ssh访问连接ec2机。

当我从终端运行这个PHP脚本时,它执行得很好,并将结束的条目写入文件中。当我从浏览器运行时,在apache错误日志中出现了这个错误

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

发现这是由于apache用户权限问题。我的猜测是对还是错,我不敢肯定。谁来帮我解决这个问题

php代码

: -

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

**最后找到了使用phpseclib的解决方案,解决了我的问题。我推荐phpseclib从PHP使用.pem文件连接amazon ec2机器,以帮助其他人分享我的示例代码。

确保.pem文件需要权限才能读取**

示例代码:

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

最新更新