如何执行一个bash脚本,使用ssh和php
问题描述:
我想用php执行我的脚本bash。我的脚本将我登录到我的服务器,然后创建我的帐户。如何执行一个bash脚本,使用ssh和php
SCRIPT="useradd martinouh --home /home/martinouh --create-home; echo martinouh:$pass | chpasswd; usermod -s /bin/bash martinouh; usermod -aG sudo martinouh; usermod -aG docker martinouh;"
sshpass -p $pass ssh -o StrictHostKeyChecking=no [email protected]$ip "${SCRIPT}"
sshpass -p $pass ssh-copy-id -i /home/Martinouh/.ssh/id_rsa.pub [email protected]$ip
但是当我通过shell_exec();
运行它确实执行脚本,但不与sshpass
的部分。
我该怎么做才能执行它?
此刻,只是为了测试shell_exec();
。
Failed to change pseudo terminal's permission: Permission denied
Failed to change pseudo terminal's permission: Permission denied
Failed to change pseudo terminal's permission: Permission denied
Failed to change pseudo terminal's permission: Permission denied
也许它在SELinux和Apache许可的事:我调用本地主机/ script.php的,包含以下
<?php
$output = shell_exec('bash select.sh');
echo $output;
?>
我会在error_log获得这个执行我的脚本?
答
现在好了,我已经禁用了SELinux,它执行该日志然后添加一个用户的部分...
但它不会做
sshpass -p $pass ssh-copy-id -i /home/Martinouh/.ssh/id_rsa.pub [email protected]$ip
我得到这个在我的网页
/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/home/Martinouh/.ssh/id_rsa.pub': Permission denied
于是,我就动钥匙,chown -R apache:apache
就可以了,我得到了这个在我的网页:
/usr/bin/ssh-copy-id: ERROR: failed to open ID file './id_rsa': No such file or directory (to install the contents of 'id_rsa.pub' anyway, look at the -f option)
我试图与-f但它不会改变
/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/.pub': No such file or directory
编辑:
它现在SELinux的工作残疾人和纠正的ssh-copy-id命令,我有将id_rsa.pub和id_rsa复制到一个目录,并将它们都复制到这两个目录中。私钥是需要的,否则我coudn't复制我的id_rsa.pub到服务器(甚至使用-f标志)。
现在我应该怎么解决这个,我们知道是什么让它工作(即使寿是不正确的方法是什么?)
给阿帕奇访问的〜/ .ssh右/? (用于ssh-copy-id)
给apache执行script/shell的权利吗? (SELinux)
答
由于错误消息引用伪终端,请尝试使用-T禁用此功能。大多数需要伪终端功能的功能都是交互式的,无论如何都不适用于这种情况。
当手动执行bash脚本时是否按预期工作? –
是的,它运行正常,当我手动运行它。 – Martinouh
你能提供更多关于你如何使用php执行的细节? –