1
我目前对我的webapp使用Codeigniter。我想通过SSH连接到EC2实例并以ec2-user身份运行一组脚本。 PHPsecLib的问题在于它不会以sudo模式运行命令。任何提示为此?我已经广泛地尝试了$ ssh-> exec。但驻留在服务器上的bash脚本无法执行。有没有更好的方法来运行驻留在服务器上的bash脚本?使用PHP配置EC2实例
我目前对我的webapp使用Codeigniter。我想通过SSH连接到EC2实例并以ec2-user身份运行一组脚本。 PHPsecLib的问题在于它不会以sudo模式运行命令。任何提示为此?我已经广泛地尝试了$ ssh-> exec。但驻留在服务器上的bash脚本无法执行。有没有更好的方法来运行驻留在服务器上的bash脚本?使用PHP配置EC2实例
的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('[email protected]:~$');
$ssh->write("sudo ls -la\n");
$output = $ssh->read('#[pP]assword[^:]*:|[email protected]:~\$#', NET_SSH2_READ_REGEX);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
$ssh->write("password\n");
echo $ssh->read('[email protected]:~$');
}
?>