【问题标题】:Execute ssh script in webpage在网页中执行 ssh 脚本
【发布时间】:2016-04-08 17:55:20
【问题描述】:

我有一个如下的 shell 脚本

#/bin/bash
SSHPASS='user'
SSHHOST='192.168.1.20'
SSHUSER='user'
ssh -t $SSHUSER@$SSHHOST << EOF
echo "Changing Directoy to Cloud"
cd /home/user/cloudit_2/it_cloud
git status
sleep 3
touch /tmp/abc.txt
ls -lah /tmp
rm -rf /tmp/abc.txt
sudo -S service ssh restart
$SSHPASS
EOF

我想从网页上运行上面的脚本,所以我在下面创建了我的 HTML 文件

<html>
<h1>Deploy</h1>
<form name="input" action="install.php">
<input type="submit" value="Submit">
</form>
</html>

install.php 如下

<?php
 $output =  shell_exec("/bin/sh a.sh");
 echo "<pre>$output</pre>";
?>

当我使用 php install.php 时,它工作正常。但是,当我尝试从网页运行它时,它没有工作。

【问题讨论】:

  • 你应该添加如何它不工作
  • 尝试使用exec 而不是shell_exec。不过这是非常危险的。
  • 1. php error_log 是否包含任何相关内容? 2. 为什么要使用没有 GET 或 POST 的表单?更好地进行简单的 AJAX 调用...
  • ALSO, note that shell_exec() does not grab STDERR, so use "2&gt;&amp;1" to redirect it to STDOUT and catch it. php.net/manual/en/function.shell-exec.php
  • 谢谢,我添加了“2>&1”并得到以下错误 - :无法创建目录'/var/www/.ssh'。主机密钥验证失败。

标签: php bash ssh


【解决方案1】:

感谢@Bozidar Sikanjic

我已按照以下步骤确定问题

mkdir -p /var/www/.ssh
sudo -u www-data ssh-keygen -t rsa

将 id_rsa.pub 复制到远程主机(我们将在脚本中 ssh 到该主机)授权的 _key 文件,这很有效。

这意味着apache用户有权使用ssh write登录远程主机。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2012-08-09
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多