Run . bat by a button

Asked

Viewed 1,495 times

-2

How can I run a . bat in the site folder by a button? It can be done in other languages as well if it is not possible by PHP alone

<button type="submit" class="btn btn-primary btn-lg">Executar .bat</button>
  • Again I repeat what I said in the duplicate question that you deleted, this would be a great security risk, if it is possible, after all, php does not run anything on the machine of whoever is opening the site, since it runs on the server only.

  • see this: http://www.somacon.com/p395.php

  • I’d like you to perform on the same Articuno, in case I’m using a VPS

  • Very dangerous this. Do not do it lightly and without knowing the risks you take

  • @Article Depends on the . bat to be executed. If this link appears on a protected page visible only to administrators and this one . bat is something to restart the server, make a backup or perform some other administrative task, it would make perfect sense.

  • Yeah, this is what @Victor Stafusa

Show 1 more comment

1 answer

1


In PHP, you can use the function shell_exec:

<?php
    $output = shell_exec('seu-arquivo.bat');
    echo "<pre>$output</pre>";
?>

It is important however first of all, make sure of the security of this, ensuring that it is not possible for any user to execute any script.

  • 2 questions, how can I make this action work when I press the button? And the bat needs to be in the folder where the index is located?

  • @Betadarknight You put this on a page that responds to some kind of POST. The script does not need to be in the same index folder and even, due to security issues, it is much better that it is not there, in which case you would use shell_exec('C:\\alguma-pasta\\seu-arquivo.bat')

  • Is it possible to start an exe program with screen only? Because it shows in the task manager that it is working but is without screen

  • @Betadarknight Run a . exe instead of a . bat should work too.

  • It runs the . exe, but the page is only loading and does not show the logs. Because it will be?

  • @Betadarknight . exe ran until the end or it just started and stayed there running?

  • It’s been running, which is why it happens, it’s a CMD that has information when it starts, I wanted it to show this information, but it keeps running

  • @Betadarknight The problem is that this function waits for the process to end. If this should not be the case, you could use that other function, but it probably won’t work yet. The best you have to do is use shell_exec('start C:\\alguma-pasta\\seu-arquivo.exe > C:\\alguma-pasta\\saida.txt'), and put another PHP page that reads the file C:\alguma-pasta\saida.txt and put it in the request (which in this other would be GET).

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.