繁体   English   中英

使用PHP配置EC2实例

[英]Configuring an EC2 instance using PHP

我目前在我的Web应用程序中使用Codeigniter。 我想通过SSH进入EC2实例并以ec2-user身份运行一组脚本。 PHPsecLib的问题在于它无法以sudo模式运行命令。 有什么提示吗? 我已经广泛尝试$ ssh-> exec。 但是我驻留在服务器上的bash脚本无法执行。 有没有更好的方法来运行驻留在服务器上的bash脚本?

phpseclib文档讨论了如何将sudo与phpseclib一起使用:

http://phpseclib.sourceforge.net/ssh/examples.html#sudo

编码:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->read('username@username:~$');
$ssh->write("sudo ls -la\n");
$output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', NET_SSH2_READ_REGEX);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
    $ssh->write("password\n");
    echo $ssh->read('username@username:~$');
}
?>

暂无
暂无

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

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