Demo

Run a shell script with an html button

But no luck. Any suggestions? EDIT- Following slhclk’s and mit’s advice: I have a php file in /var/www that I point my web-browser to. The contents of the file is as follows:

If I type /home/aa/scripts/test.sh in shell, the script is able to execute. However, when I point my web-browser to http://example.com/screen.php (which has the code above), I see a blank page and the script hadn’t executed. Both have execute permissions. Why doesn’t it work?

It would be interesting to know what kind of web server this is. For example it could be a home linux server, a windows machine with XAMPP or a standard website mass hosting offer with a domain name from a big hoster?

This is really a Dup of this, even asked by same user»>> stackoverflow.com/questions/6235785/…, where I posted my working answer. Felt they should be linked.

5 Answers 5

What you are trying to do is not possible that way.

Note that there are always two sides to that: The client side and the server side. Is the script on the client computer or on the server?

Читайте также:  Php string parsing functions

If it’s on the client: You as the visitor are only seeing an HTML website. onClick will only be able to launch JavaScript (or other scripting languages), but not any arbitrary shell script that resides on your computer. HTML scripts only run in the browser and can only do limited things. Most importantly, they can’t interact with your computer.

Think about it: How would the browser know how to open the file? Don’t you think this would be a security issue as well – a plain website triggering the execution of scripts on a client’s computer? What if there was something like onClick(‘rm -rf /home/user’) ?

An alternative would be to run a Java applet, if you want code to be executed on the client, but this not exactly the same and it’s something really complicated. I don’t think it’s necessary to explain this in detail.

If the script is on the server: If you want to run a script on the server side and have the user trigger its execution, then you need to use a server side programming language. Just HTML won’t do it, because it’s more or less a static file. If you want to interact with the server, you could for example use PHP.

It has the exec function to run a command line script that is stored on the web server. So basically, you could write exec(‘/path/to/name.sh’); and it would run the script on the server.

However, just putting this into onClick is not enough here. If you don’t know about PHP and server side web programming yet, you might want to read a few tutorials first and then come back with a more specific question.

If you have a php file with the appropriate exec(. ) command, make sure the script has execute permissions set not only for the user but also for the group the web server is in, so in the simplest case just 777 .

In case of trouble check for the return value of the script with echo exec(. ); to see if there are any errors.

You can also run the script from the command line and not from the browser with php /path/to/file.php .

Источник

How to run Shell script from HTML?

I’m somewhat new to Ubuntu Linux. I need to run the shell script from a html code. As I read somewhere like using Apache2 to generate a html and somehow I’ve succeeded to run a sh file from html, but I’m unable to see the command prompt which executes the commands in the sh script. It’s running may be as a daemon. So can any body help me how to make it visible?

3 Answers 3

This is a totally wrong approach. The HTML file does nothing, it is a Markup language, that instructs a browser how to show you different elements. What you need it is a PHP, ASP, CGI, etc script that uses the system’s shell to run arbitrary commands.

thanks for the reply Frantique.. i’ve used ‘CGI’ only for this. but unable to view the output. is there any way we can make the terminal visible.

When you run a shell script as CGI, which is what I believe you are doing (or trying to do), it is run from the apache or other web server daemon and its output isn’t visible anywhere. You can redirect its output to a file, for example by replacing the original script with one that runs the script and saves the output:

#!/bin/bash ./original.sh | tee --append file-to-save-to 

(You should probably also update your question to show what your objective is, in addition to the problem you have encountered.)

Thanks for the reply otus, my intention is to make some build system with graphical user interface. rather than typing commands to build some code(ex: like make xxxx etc.) i want to create HTML buttons to be clicked for building appropriate module. But by using CGI i’m unable to see the output for ‘make’ and even i’m unable to know whether it is running background or not. so any one can help me regarding this.. is there any simple approach to follow other that what i’m using. thanks in advance

outus, i’ve tried as u said to catch the output to some file, but the output is getting stored in the file only when every thing goes correct, if some error has occured (in the command mentioned in original.sh) then the file looks empty. -(

suresh, errors are written to stderr, so you’ll need to redirect that to either a different file or the same: e.g. ./original.sh 2>&1 | tee —append file

Источник

Demo: How to invoke a Shell Script on a HTML Page being served by Apache on Linux Machine

Step 1: Put your shell script in cgi-bin directory.
You can find it using command: grep ScriptAlias /etc/httpd/conf/httpd.conf
On my RHEL6 machine it is defined as: /var/www/cgi-bin
I am using a very simple test script for this tutorial. File Name and content is provided below.

File Name: test_script.sh
File Content:

#!/bin/bash
# get today’s date
OUTPUT=”$(date)”
# You must add following two lines before
# outputting data to the web browser from shell
# script
echo “Content-type: text/html”
echo “”
echo “”
echo “Today is $OUTPUT

echo “You can do anything with this Schell Script 🙂 ”
echo “Good Luck!”
echo “”

Step 2: Put your HTML page in DocumentRoot directory.
You can find it using command: grep DocumentRoot /etc/httpd/conf/httpd.conf
On my RHEL6 machine it is defined as: /var/www/html

Demonstration for calling Shell Script from a web page

Step 3: Go Ahead and test in browser.
http://HOSTNAME/myPage.html

Execution Screenshot

Click on Link and you will see that shell script is executed and result is displayed on web page.
Similarly, click on HTML button and you will see that shell script output is displayed on web page.

NOTE: You can find host name of your machine using command: hostname

Prerequisites –
1. Apache should be installed on Linux machine
2. httpd service should be running. (service httds status)

I hope it is useful for you.

Thank You so much for your valuable time!

Источник

Running linux commands through html

I am successful to do this conversion in the terminal by using: The issue is how can I make the convert button in the html file run the script above to do the conversion and send the converted file back to the webpage . because I have to use plink to do the conversion. run html file from terminal how to open html file in terminal

Running linux commands through html

I want to create a basic html file that has an upload button for 3 files that are in a folder(.bed, .bim, .fam), and a convert button to convert those input files into another file (.ped, .map)

I am successful to do this conversion in the terminal by using:

plink --bfile inputFile --recode --out newFile 

The issue is how can I make the convert button in the html file run the script above to do the conversion and send the converted file back to the webpage . because I have to use plink to do the conversion.

Thanks and any help is much appreciated !!

There is no way to do this directly.

The closest you could come would be to run an HTTP server and write a server side program (e.g. via Perl + PSGI + FastCGI, Node.JS + Express or even Bash + CGI ) which acted as a wrapper around those commands.

You could then have an HTML document include a which, when submitted, would trigger the HTTP request that would cause the server to run that program.

Linux — How can I run a html file from terminal?, Navigate to the directory containing the html file. And Simply type the following on the Terminal:-. pushd .html; python3 -m http.server 9999; popd; Then click the I.P. address 0.0.0.0:9999 OR localhost:9999 (Whatever is the result after executing the above commands). Or type on the terminal :-. Usage examplebrowsername Feedback

How to Run HTML Code using Notepad in localhost

In this very #short video i will explain you how to run html , css file or code in localhost with notepad in chrome browser . This is very easy and helpful tu

Html to run a system command

I am trying to run this command to create a folder inside my raspberry pi but it isn’t working. I am not good with PHP or HTML. I googled and i am unable to solve, so posting it here. If it is repeated and the solution exists. please do provide me the link. TIA Code:

    if (isset($_POST['TestB'])) < shell_exec('sudo mkdir /www/test1'); >?>  
Test case 1 Test case 2

Have you noticed you are try to run a command with sudo , that means the user www-data must to be included in the /etc/ sudoers file .

I’ve tried your code, it works if i remove the ‘sudo’ and if i give permissions in the /www directory.

I think you need to configure the user in the sudoers file.

An example of my sudoers file:

#WWW-DATA User_Alias WEBUSER = www-data Cmnd_Alias CMDCOMMAND = /usr/sbin/asterisk, /sbin/iptables WEBUSER ALL = NOPASSWD:CMDCOMMAND #Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL 

You need to replace the CMDCOMMAND, with the commands your webserver will run as root. In this case mkdir .

Remember you need to go to /etc/ directory and use visudo -f sudoers to edit the sudoers file correctly.

P.S: If you want to test if your webserver could run a command, you can:

After it will be a sh console.

You could run your command before adding in code.

You can create a directory with PHP without calling out to the shell. PHP has its own mkdir function .

and do the same for the second mkdir command.

The first argument is the directory to create the second is the mode (permissions).

If you really want to create this directory in /www (see note about security below) you will need to edit the sudoers file for the www-data user as described in Ivan’s answer or create the directory and change the owner. You can create the /www directory with the following:

and change the ownership with the following:

Note: Giving www-data sudo privileges creates a security hole. You should probably create the directories you need in the /var/www/html/ directory where permissions are not an issue and sudo is not required. If you do not want these directories accessible from the web, I would suggest creating them in /var/www and changing the owner.

Running linux commands through html, The closest you could come would be to run an HTTP server and write a server side program (e.g. via Perl + PSGI + FastCGI, Node.JS + Express or …

Run html file from terminal

Linux — Html to run a system command, You need to replace the CMDCOMMAND, with the commands your webserver will run as root. In this case mkdir. Remember you need to go to /etc/ …

How to run an html file in terminal

how to open html file in terminal

Create an HTML file on your mac Open your terminal Run the command "open myFile.html" or navigate to it with Finder and right click > open with Brave See a search page load

Apache — How to run my html file with apache2, In my case where I have installed and running the Apache 2 and if I type localhost/ into the browser it opens the Apache 2 Ubuntu default Page.. Now …

Источник

Оцените статью