I figure out how to execute the input to the cmd program with php using exec function ('cmd.exe')
most of the tutorials available on the internet suggest execute commands using the command line argumen.tapi cmd.exe (DOS) does not have a command line argument
so basically I want to set the (DOS) program like cmd enter input into it without having to use a parameter cmd argument, as we typed directly into cmd itself.
I made a (DOS) program using C ++ language, I want a program that can be run in php website without having to create a parameter in advance, as we typed directly into the program, live stream
code that I use
<?php
$inputcmd = $_POST['inputcmd'];
$output = shell_exec('cmd.exe $inputcmd');
echo "<pre>$output</pre>";
?>
<form method="POST" />
<input type="text" name="inputcmd" />
<input type="submit" value="submit" />
</form>
dos program that I created does not have prameter, so I want to type directly into it blends into the input output php
You can generally get cmd to execute your code as arguments if you use something like:
C:\Users\PaxDiablo\Documents> cmd.exe /c "echo hello"
hello
That's from an actual command interpreter window but the same rules apply anywhere you can run cmd.exe.
I suppose I don't need to mention that it's a spectacularly bad idea to allow some random Joe to execute arbitrary code on your server. So, if you haven't thought that through, give it at least a couple of minutes.
Related
I'm having some problems with PHP SSH2 commands.
So I have succesfully connected my dedicated server with my app, but I have no idea how to run command with that.
I'm using phpsclib ( http://phpseclib.sourceforge.net ), when I write something like
echo $ssh->read('username#username:~$');
$ssh->write("ls -la\n");
echo $ssh->read();
It will give me a list from root, but the problem is I don't want to be in root.
I need to cd /another_file and execute command that will start some game server.
I tried something like
$ssh->write("cd /another_file");
$ssh->write("my command here");
But no success, it's showing commands in same line.
Any ideas how to do that?
You have several options.
Chain the commands using $ssh->write():
$ssh->write("cd /another_file && ls -la\n");
...or...
$ssh->write("cd /another_file; ls -la\n");
Chain the commands using $ssh->exec(). Works much the same way as above except that you don't need to append \n to your command and you don't need to bother with the $ssh->read().
Do multiple $ssh->write() calls, which is what your current attempt is attempting to do. But it's doing it wrong:
$ssh->write("cd /another_file");
$ssh->write("my command here");
In your first code snippet you're appending a \n but you're not doing so on these lines. You need to because, when you're in an interactive shell, the way the shell knows that you're done typing the command out is because you hit the enter button. That's how Linux knows you're done typing and are ready for your command to run.
You also need to read the prompt before running the next command. eg. $ssh->read('[prompt]') or whatever is appropriate for your system. I suppose you could just omit passing a parameter to $ssh->read() all together, as you do in your first code snippet, but then it'll take 10s or so for $ssh->read() to return any output when it could have alternatively been returning output instantly.
I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.
I hava a bash file that can be executed when I run it from the terminal using php -a (interactive shell), by typing:
shell_exec('sh /home/oceanview/run_test.sh');
I have created a button and when this button is clicked the bash file should be run
<form action="Control.php" method="post">
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
shell_exec('sh /home/oceanview/run_test.sh');
}
?>
But nothing happens when I press the button. I have tried chmod +x and chmod 0777 already and there is no error showing in log.
I have to add that the interactive shell asks for my password when I run the shell_exec command in the terminal.
Under a web post request, you aren't going to be able to run a script that's interactively requesting your password (at least not without a lot of additional infrastructure, like an expect script). How do you expect to be typing in your response, anyway?
Most likely what is happening is that the process is exiting on detecting that it isn't attached to an interactive terminal. You wouldn't be able to see any output it's producing anyway, since you aren't capturing or using the result from shell_exec().
You can use something similar to this in order to input your password and then pass it off to the shell command
<form action="Control.php" method="post">
<label>Password: <input type="password" name="pwd"></label>
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
$command = sprintf(
'echo %s | sh /home/oceanview/run_test.sh',
escapeshellarg($_POST['pwd'])
);
echo shell_exec($command);
}
?>
You can test this out from a command line by using
echo 'mypasswordhere' | sh /home/oceanview/run_test.sh
So I'm working on an application of my own where i need to execute shell commands, and even execute csh scripts on button clicks.All the HTML code is doing is displaying results from some sensors i have set up on my arduino via firefox, so im basically using it as a GUI. The HTML page wont ever be online, its purely running on my own machine.
I understand that its not straight forward as having HTML run shell commands on a live webpage is a security issue, but i have been reading about being able to execute shell commands using php scripts, but as it is running on a standalone PC i dont know how to integrate it into running a command from a button click on the page.
Infact i have no experience in using php at all, and i wouldnt even know where to start getting it to run through my HTML code.
Help!
edit:
If not php, is there any other way i can run a command such as 'ls' from a button click on the html page?
Oli
You can use the exec php function, and execute your scripts like this exec('/path/myscript.sh')
But surely you have to setup your php/apache in a correct way. Just try to look for some basic web-server configuration tutorials.
php command exec should execute a command and retrieve standard output.
<?php
$result = exec("wc -l /etc/issue");
print($result);
?>
With that print command you can print any html-text, that will be inserted into the page the client receives and displays. So simply surround that php-snippet with you actual web page.
General idea, you can create a HTML form with buttons that correspond to specific shell commands ( I understand this won't be LIVE on the web, so the security risks might not be an issue).
From there, you would receive the form input in PHP (you can use a framework like Laravel which makes this much easier to handle frontend/backend communication - http://laravel.com/docs/4.2/html http://laravel.com/docs/4.2/requests)
A quick example with HTML and PHP:
<form id="arduino" method="post" action="{{ url('arduino/control') }}">
<input type="text" name="command"/>
<input type="submit">
</form>
<?php
Route::post('arduino/control', function () {
$command = Input::get('command');
exec($command);
});
I have a command line application I need to execute from my PHP web application. Say the command is the following:
foo -arg1 -arg2 -arg3
Based on certain conditions, the command line application will prompt user to hit the enter key (e.g. "Please press enter to continue.").
From my PHP application, how do I execute the command line AND send the enter key as response to the prompt?
I'm developing on WAMP. Production code is LAMP.
That's what the 'yes' program is for. It dumps an endless stream of 'y\n' (or whatever you tell it to via arguments) to the program. It exists for this purpose (answering 'yes' to "do you want to continue" prompts).
shell_exec('yes | foo -arg1 -arg2 -arg3')
You will really need to open a process handle and parse the programs output and write appropriate output in response.
Check out the expect extension though, which can make this sort of thing easier.
$value = fgets(STDIN);
This will allow the user to enter in a value, which you can then access via $value.
Have you tried echo "\n" > foo -arg1 -arg2 -arg3 ?