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);
}
?>