在网页中执行ssh脚本

时间:2016-04-08 07:13:21

标签: php bash ssh

我有一个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时,它工作正常。但是,当我试图从网页上运行它时,它没有用。

1 个答案:

答案 0 :(得分:0)

感谢@Bozidar Sikanjic

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

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

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

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

相关问题