As stated by Luke you need to use a server side language, like php. This is a really simple php example:
Save this as myfilename.php and place it on a machine with a web server with php installed. The same thing can be accomplished with asp, java, ruby, python, .
This answer works on my Ubuntu desktop with apache2 and php5 installed. Following is some additional information to help better understand this answer: 1. «myfilename.php» may be placed under the web server’s root directory (DocumentRoot) which usually is «/var/www». 2. «myfilename.php» contains the above code, plus other HTML code. 3. The web client uses «/myfilename.php» as the HTTP address.
we don’t php installed can it possible any other way like javascript. or without php install run this script.
Shell can also be a server-sided language. Why not just case «$/$» in (GET/run=true) /path/to/name.sh;; esac ? Then let web server software call this, I guess.
This is really just an expansion of BBB’s answer which lead to to get my experiment working.
This script will simply create a file /tmp/testfile when you click on the button that says «Open Script».
The actual HTML Website with a button.
A php script which executes the script
A Script
The File Tree:
root@test:/var/www/html# tree testscript/ testscript/ ├── index.html ├── testexec.php └── test.sh
if its possible to run the exe from sh file ? my environment is windows os. i want to execute the exe from browser i searched past three days nothing help me
Could you please expand on the need for ?success=true’ after the .html part on step 2? Maybe it can be removed?
PHP is likely the easiest.
Just make a file script.php that contains and send anybody who clicks the button to that destination. You can return the user to the original page with header:
This is how it look like in pure bash
#!/bin/bash echo Content-type: text/html echo "" ## make POST and GET stings ## as bash variables available if [ ! -z $CONTENT_LENGTH ] && [ "$CONTENT_LENGTH" -gt 0 ] && [ $CONTENT_TYPE != "multipart/form-data" ]; then read -n $CONTENT_LENGTH POST_STRING "|tr '&' ';'` fi eval `echo "$"|tr '&' ';'` echo "" echo "" echo "" echo "" if [[ "$vote" = "a" ]];then echo "you pressed A" sudo /usr/local/bin/run_a.sh elif [[ "$vote" = "b" ]];then echo "you pressed B" sudo /usr/local/bin/run_b.sh fi echo "" echo "
How to Run Bash Script Using HTML Button in The Browser?
When we want to run a bash script, we usually run two methods. The first method is to access the server where the bash script is located or use the crontab for the second method. Is there any alternative method other than these two methods? The answer is yes. We can use one method besides using the two methods above to run a bash script that is using the HTML button in the browser.
How to run bash script using HTML button in the browser?
We can run a bash script using the HTML button in the browser using the shell_exec() or exec() or system() functions. The three functions will return the same value if we use these functions. The differences are:
shell_exec() returns the complete output as a string. The string can be NULL when an error occurs or if the command produces no output.
exec() returns the last line of the executed command as a string. NOTE: Use exec() when you require access to the program exit. shell_exec() does not provide the program exit code information.
system() is like shell_exec() but displays output directly (without using echo() or print()).
For example, we can see the OS version of our server and the memory used in the browser by typing a PHP script (for example, we name it shell.php) as below:
Open a browser and it should look like this:
Running Linux command and display it in the browser
As seen in the image above, using the PHP command, we can run the Linux command and display it in the browser.
If we want the bash script to work after we press the HTML button in the browser, then we first create an HTML file (for example we name it index.php and I create it in the test folder) and copy the script below:
Experiment to run bash script after pressing html button in browser
Then we create a test.php file like the script below:
After that, create a bash script with the name bash_script.sh as below:
Run the following command so that we can execute the file:
Open your browser and it should look like this:
Click the Click Me! button
Click the Click Me! button then there should be a display like below:
After you click the Click Me! button
If we want the bash command to perform Linux commands such as copying files or deleting files, then we have to run the access control list. For example, we want to see at what time the user presses the Click Me! That. Then in the bash_script.sh file copy the following script:
#!/bin/bash cat /etc/*release | grep PRETTY_NAME echo free -m echo echo "The user has been clicked the button - `date`" >> log.txt
Then run the following command:
setfacl -Rm u:apache:rwx /var/www/html/test
After that refresh the web and click the button, then in test folder should form a log.txt file.
If we want after clicking the button, the web page redirects to the page that we have specified, then we first create a file that is the destination of the redirect, for example, named finish.html. Copy below script for the finish.html file:
The process has been done. Please check!!
And copy the below script in the test.php file:
After you click the button, PHP will process the bash script and then the website will redirect to another page.
Redirection to another page
If we use the setfacl command on the website folder, we recommend that the public does not use the website, but the website is only for internal purposes for security reasons.
How can I execute a python script from an html button?
I have a number of python scripts that I have saved on my computer, out of curiosity I have created a html file that just has one button. Instead on going into the terminal and running python , I would like to make it so when that button is clicked, is kicks off one of my python scripts. Is this possible/how can it be done? for example purposes we’ll call the script to be run MYSCRIPT.py . I’ve done a little research but have come up with nothing promising. I’m aware the following code is incorrect but just for a starting point here is my html file.
Webpages are not allowed to run shell commands, as that would e very dangerous. Image going to a site that can run any command in your shell. What you would need is a server, that you send a request to and the server runs the command there.
7 Answers 7
There are various ways to make it done, very simple technique with security peace in mind, here might help you
1. First you need to install Flask pip install flask or pip3 install flask in your command prompt, which is a python microframework, don’t be afraid that you need to have another prior knowledge to learn that, it’s really simple and just a few line of code. If you wish you learn Flask quickly for complete novice here is the tutorial that I also learn from Flask Tutorial for beginner (YouTube)
2nd create another subfolder inside previous folder and name it as templates and create index.html file inside it
My Website
3. To run, open command prompt to the New Folder you created earlier, type python server.py or python3 server.py to run the script, then you will get response in your terminal/command prompt that server is running on http://127.0.0.1:5000 or any other ports. After that you go to browser type localhost:5000 or http://127.0.0.1:5000 , then you will see button. You can click and route to destination script file you created.
Hope this helpful. thank you.
Since you asked for a way to complete this within an HTML page I am answering this. I feel there is no need to mention the severe warnings and implications that would go along with this .. I trust you know the security of your .py script better than I do 🙂
I would use the .ajax() function in the jQuery library. This will allow you to call your Python script as long as the script is in the publicly accessible html directory . That said this is the part where I tell you to heed security precautions .
In addition .. It’s worth noting that your script is going to have to have proper permissions for, say, the www-data user to be able to run it . A chmod , and/or a chown may be necessary.
this would mainly be dangerous if this is a script with a web server correct like flask or django? If this was just an simple html file and python file in the same directory running a script on a local machine. this would not be dangerous or am I missing something
The only security risk is that which is present for all server-side files. If someone can access the file directly and manipulate inputs / injections to get a result that may display sensitive information like the server directory structure or tables within the database.
— It’s the programming within the .py file that can be the security risk if not properly locked down. Usually if it’s already part of a framework, most of those security concerns are already addressed within the framework.
understood. so assuming the only parts involved in the project are a simple html file with a button and a python script that runs onclick from within a local machine. the biggest security concern is someone unauthorized getting access to that file.
This does not work. I recieve a 405 error reponse. I think I need a backend solution in order to work, because calling a .py file from the frontend it doesnt work.
Best way is to Use a Python Web Frame Work you can choose Django/Flask. I will suggest you to Use Django because it’s more powerful. Here is Step by guide to get complete your task :
from . import views path('', include('blog.urls')),
Now go to parent directory and execute manage.py
python manage.py runserver 127.0.0.1:8001
This would require knowledge of a backend website language.
Fortunately, Python’s Flask Library is a suitable backend language for the project at hand.
Check out this answer from another thread.
I’ve done exactly this on Windows. I have a local .html page that I use as a «dashboard» for all my current work. In addition to the usual links, I’ve been able to add clickable links that open MS-Word documents, Excel spreadsheets, open my IDE, ssh to servers, etc. It is a little involved but here’s how I did it .
First, update the Windows registry. Your browser handles usual protocols like http, https, ftp. You can define your own protocol and a handler to be invoked when a link of that protocol-type is clicked. Here’s the config (run with regedit )
With this, when I have a link like ProjectX , clicking it will invoke C:\_opt\Dashboard\Dashboard.vbs passing it the command line parameter open:ProjectX.docx . My VBS code looks at this parameter and does the necessary thing (in this case, because it ends in .docx, it invokes MS-Word with ProjectX.docx as the parameter to it.
Now, I’ve written my handler in VBS only because it is very old code (like 15+ years). I haven’t tried it, but you might be able to write a Python handler, Dashboard.py , instead. I’ll leave it up to you to write your own handler. For your scripts, your link could be href=»mydb:runpy:whatever.py» (the runpy: prefix tells your handle to run with Python).