将表单数据作为变量传递给docker命令

时间:2019-01-27 01:32:10

标签: php linux docker ansible

我正在尝试通过网页运行一个无用的Docker容器,但没有找到解决方案,只是部分解决了。

我有index.html

<html>
<body>

<form action="adduser.php" method="get">
Server: <input type="text" value="<?php echo $name?>" name="name"><br>
<input type="submit">
</form>

</body>
</html>

adduser.php

<?php
  chdir("aatool"); 
  echo getcwd();
$hosts = $_POST["name"];
echo "$hosts";
// $hosts = escapeshellarg($hosts);
//echo ".escapeshellarg($hosts)";
$output = shell_exec('sudo docker run -i --network=host centos7-ansible ansible-playbook --private-key=ky.pem --extra-vars admin_password=test -e hosts=".escapeshellarg($hosts)." chpass.yml');
  echo "<pre>$output</pre>";
?>

网页输出:

/var/www/html/aatool PLAY RECAP *********************************************************************

Docker容器日志:

PLAY RECAP *********************************************************************

Apache error_log:

[:error] [pid 40367] [client x.x.x.x:61812] PHP Notice:  Undefined index: name in /var/www/html/adduser.php on line 4, referer: http://x.x.x.x/

如果我直接传递hosts值,例如hosts = all,则一切正常,但是如果我尝试从php表单中获取值,则它不起作用。我尝试了所有可以找到的东西,这段代码给了我最好的结果,但是仍然无法正常工作。 我希望我能在这里找到一些帮助

谢谢。

1 个答案:

答案 0 :(得分:1)

您的FORM正在通过GET方法提交,但是您的代码检查$ _POST变量。尝试从$ _GET设置主机,或将FORM更改为POST。