首页 > 解决方案 > 如何修复未使用表单输入执行的 php 代码?

问题描述

我是新来的 php。

我正在运行一个简单的 php 代码来 ssh 到远程服务器并执行一个长命令:

ldapsearch -b o=Devices -h 24.33.42.43 -D "uid=aaa_user,ou=AppUsers,o=services" -w "authAuthAcc01" rrWiFiDeviceMAC="A4-F1-E8-24-6B-28" 

对于特定的 MAC 地址A4-F1-E8-24-6B-28。我用一个表格输入一个MAC地址,然后提交执行ssh到linux服务器,再执行上面的命令。echo $CMD将在指示$var=$_POST[]作品的页面上显示完整的命令。但是,代码不会运行到 ssh2 函数。这就像$var变得空虚或什么的。但是,如果我硬编码$var并从 PHP 命令行运行,它就可以工作。请问,我做错了什么!

谢谢你的帮助。

<form  action="MAC_search.php" method="post">
    Enter MAC Address to Search:<input type="text" name="mac">
    <input type="submit" name='submit' value ='Search' />
</form>



<?php
   if ($_POST["submit"] ){
   $MACD=$_POST['mac'];
   //$MACD='F8-62-14-E9-F7-51';  *****Hardcoding here works*****
    $CMD = "ldapsearch -b o=Devices -h 24.33.33.5 -D \"uid=a . 
    aa_user,ou=AppUsers,o=services\" -w \"authAuthAcc01\" 
    rrWiFiDeviceMAC=\"$MACD\"";

    echo $CMD; **** Here echos on the page also *******
    $connection = ssh2_connect("x.x.x.x", 22);        
    ssh2_auth_password($connection, $username, $password);
    $stream = ssh2_exec($connection, "$CMD");
    stream_set_blocking($stream, true);
    $output = stream_get_contents($stream);

   $resultfile = fopen("data.txt", "w") or die("unable to open 
    file");  
    fwrite($resultfile, $output);
    fclose($myfile);
  }
?>

标签: phpformsfilessh

解决方案


推荐阅读