从PHP SSH访问EC2PHP、SSH

2023-09-11 09:11:14 作者:踩到姑娘滴小蘑菇

从本地Ubuntu Linux操作系统的机器通过ssh访问连接EC2机。

当我运行这从终端它执行罚款,并写入尾项文件的PHP脚本。当我从浏览器中运行的Apache的错误日志中得到这个错误

SSH:无法解析主机名Proxy2将:名称或服务不知道

发现其由于Apache用户权限的问题。我的猜测是对是错我不知道。任何一个可以帮助我解决这个问题。

PHP code: -

 < PHP

$ SS ='SSH Proxy2将.'tail -n 3 /out/speed_log.txt.1'。 > proxy2temp1;
系统($ SS);
?>
 

解决方案

**终于找到使用phpseclib的解决方案,解决了我的问题。我建议phpseclib连接Amazon EC2的机器从PHP uing .pem文件,以帮助其他人分享我的例子code。

ssh远程访问linux 服务器

确认.pem文件需要权限读取**

样品code:

 包括(网/ SSH2.php');
包括(地穴/ RSA.php');

$键=新CRYPT_RSA();

$键盘> LOADKEY(的file_get_contents('/ pathtokey.pem'));


$的ssh =新Net_SSH2('ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com');
如果($ SSH->!登录('用户',$键)){
退出(登录失败);
}

回声$ SSH->执行exec('尾巴-n 3 /out/_log.txt.1');
 

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