LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   How do I execute a bash script from a link on a web page? Is there any way to do this (https://www.linuxquestions.org/questions/linux-desktop-74/how-do-i-execute-a-bash-script-from-a-link-on-a-web-page-is-there-any-way-to-do-this-794425/)

digilifellc 03-10-2010 07:49 AM

How do I execute a bash script from a link on a web page? Is there any way to do this
 
I have a website that I can host and store my scripts. I want to be able to just click a link and have the computer that is clicking the link do one of two things:
1. open gnome-terminal and execute the script that is stored on the remote server, or
2. download the script, make it executable, and run the script locally
3. if I need a hybrid of the two, that's fine by me.

I have searched possible answers and solutions using Google searches on bash scripts, executing php, html codes, and anything else I could think of with no success. Thanks in advance.

MS3FGX 03-11-2010 08:20 PM

In a word, no.

Could you imagine how dangerous it would be to even browse the Internet if it was possible for a link on a web page to start programs and run commands on your local machine? That is one of the reasons ActiveX (and IE in general, really) have been plagued with security issues.

You could obviously put a link on the page to download the script, but executing it would be an exercise for the user of the local machine.

meingbg 03-22-2010 08:48 AM

Quote:

Originally Posted by MS3FGX (Post 3895088)
Could you imagine how dangerous it would be

A web-browser is not supposed to allow such behaviour. However, it is a fairly simple task to write a script that allows you to do this without a web browser, if all you have is a list of downloadable scripts hosted on some web page.

theYinYeti 03-22-2010 09:29 AM

It's indeed insane at first view. Yet, supposing you have a special use case where this is secure (hard to imagine), then it is easy:
1- have your server (eg: a PHP script) sent the script with a “.bash” extension (for example) and an custom MIME type: “text/x-bash-script”
2- your browser will ask you what command to use for the “text/x-bash-script” type; point it at /bin/bash.

Yves.

francoisrv 03-09-2011 07:56 AM

use shell_exec
 
Kinda l8 reply but you could also do this from a PHP script:

<?=shell_exec('bash /path/to/my/script')?>

This is to the extent that shell-interaction has not been disabled by your sysadmin

If your exec function receives input (such as GET or POST), make sure you use the required validation and data sanitation and proper file permissions so nobody can do a bash injection or some other kinda bash bomb on you!

pwalden 03-10-2011 09:56 AM

You could create a Launcher, such as the one I did here.

The Launcher would run the script in a terminal. You would drag the web link from the browser and drop it on the Launcher. The launcher script would download the web link script and then execute it.

The launcher script would be something like this (I did not debug this).

Code:

#!/bin/bash
if wget $1 -O webscript.bash;
then
  read -p 'want to execute script? [y|N]' -t10 -n1;
  if [[ $REPLY == [Yy] ]]
  then
      bash webscript.bash;
  fi
  rm webscript.bash;
fi
#hold the terminal window open for a short while.
sleep 10;


francoisrv 03-11-2011 02:56 AM

using cgi with Apache could also do the trick if your bash script behaves differently according to the web context such as GET or POST. google 'apache cgi bash'


All times are GMT -5. The time now is 10:22 PM.