douwen1937 2018-12-24 04:12
浏览 48

如何修复不使用表单输入执行的PHP代码?

I am new here and to php.

I am running a simple php code to ssh into a remote server and execute a long command:

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" 

for a specific MAC Address A4-F1-E8-24-6B-28. I used a form to input a MAC Address, then submit to execute ssh into the linux server before executing the command above. echo $CMD will display the full command on the page indicating $var=$_POST[] works. However, the code won't run to ssh2 function. It's like $var becomes empty or something. But if I hardcode $var and run from PHP command line it works. Please, what am I doing wrong!

Thanks for your help.

<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);
  }
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?