Page 1 of 1

Run terminal command from webpage

Posted: Thu Nov 15, 2012 4:30 pm
by cazz
Hi again :)

If I like to run a terminal command from my webpage, what is the best way then?

I like to have button to push if for example I wan't to restart my raspberry pi or if I like to run "wakeonlan 00:00:00:00:00:00"

Re: Run terminal command from webpage

Posted: Thu Nov 15, 2012 5:13 pm
by jecxjo
You'll want to look into writing CGI scripts. This is code that runs when a site is loaded.

For instances on my Pi I have the following file /www/ping.cgi

Code: Select all

#!/bin/bash

echo "Ping" >> /tmp/cgi-called.log

cat << EOF
Content-type: text/html

<html>
<head>
<title>Cgi Script</title>
</head>
<body>
CGI Script ran, check out /tmp/cgi-called.log to see how many times
</body>
</html>

EOF
Depending on your webserver there may be a special CGI directory where executable will live. Most systems also have a special user that runs as the webserver. This user typically does not have access to system operations like rebooting, which is by design. If someone was able to change your cgi script and run it you wouldn't want them to be able to have root privileges. But there is always ways around these restrictions, just not always safe.

Re: Run terminal command from webpage

Posted: Thu Nov 15, 2012 7:00 pm
by cazz
Ahh ok, well I have try to make my lighthttpd to have CGI support but I have no idea if I have made it works or not :)

I was going to post a topic here later about that :)

Re: Run terminal command from webpage

Posted: Thu Nov 15, 2012 8:58 pm
by AndyD
The placeholder page for lighttpd (/var/www/index.lighttpd.html) has the following information
CGI scripts are looked for in /usr/lib/cgi-bin, which is where Debian packages will place their scripts. You can enable cgi module by using command "lighty-enable-mod cgi".
That was all I needed to do to get cgi working for me.

Re: Run terminal command from webpage

Posted: Thu Nov 15, 2012 10:18 pm
by cazz
ok but have you edit the config file?

I have done that and now I have use "lighty-enable-mod cgi" and " /etc/init.d/lighttpd force-reload"
I did have to remove "mod_cgi" and run it again and it say "OK" but still it can't find the cgi file.


http://www.raspberrypi.org/phpBB3/viewt ... 26&t=22986

Re: Run terminal command from webpage

Posted: Fri Nov 16, 2012 7:46 am
by AndyD
Hi Cazz,
No I didn't edit the config at all. I just made a cgi-bin directory in /var/www/ and put my script in there (I am using perl but I assume it is all the same).
I wasn't sure why they were talking about /usr/lib/cgi-bin, but I didn't want my cgi scripts in a different location to my HTML files.

Re: Run terminal command from webpage

Posted: Fri Nov 16, 2012 8:15 am
by cazz
Hi AndyD
Ohh ok then I have to try that when I get home :)

Re: Run terminal command from webpage

Posted: Fri Nov 16, 2012 7:05 pm
by Ripster
Hey guys.
I ran into a problem. I am also trying to control my camera from webpage I'm using php to run a script or command.

Code: Select all

<html>
  <head>
    <title>Output of my bash script</title>
  </head>
  <body>
    <h1>Output of my bash script</h1>
<pre>
<?php system("gphoto2 --capture-image", $rc); ?>
</pre>
    <br>
    <?php echo "Return Code: {$rc}\n"; ?>
  </body>
</html>
I can run gphoto2 commands or sh scripts. The problem is that I have a camera that needs usbreset and I can't get it to work over webpage. Everything works from command line. Has anyone solved this or can help. thx

Re: Run terminal command from webpage

Posted: Fri Nov 16, 2012 7:41 pm
by cazz
Strange
I did make a clean debian and follow this guide (I did follow this before)
http://simonthepiman.com/how_to_setup_a ... upport.php

after that I did run "lighty-enable-mod cgi" and " /etc/init.d/lighttpd force-reload"

After that I did upload the ping.cgi file and I change to +x

But it show nothing, just a plain blank screen.
I even look inside the log "/tmp/cgi-called.log" nothing there.

Re: Run terminal command from webpage

Posted: Sat Nov 17, 2012 2:57 am
by AndyD
I would try a simple cgi script first (e.g. http://perl.about.com/od/perltutorials/a/hellocgi.htm) before trying more complicated programs/scripts. One issue may be that the lighttpd demon is run as user www-data. So all the cgi-bin scripts are also run as user www-data, which could mean you are possibly running into permission problems.

Re: Run terminal command from webpage

Posted: Sat Nov 17, 2012 9:04 am
by cazz
Hmm strange

I have change the owner of cgi file and even the group owner of the file but still does not show anything.

I wonder if that have to do that PHP run mod_fastcgi, what I understand lighttpd run PHP as CGI?

Re: Run terminal command from webpage

Posted: Sat Nov 17, 2012 10:09 am
by AndyD
Hi Cazz,
I didn't mean the read permissions on the files, but permission to perform some operations. For example the command to get the cpu (?) temperature on the pi

/opt/vc/bin/vcgencmd measure_temp

fails when run as user www-data.

Code: Select all

pi@raspberrypi ~ $ sudo su - www-data
$ /opt/vc/bin/vcgencmd measure_temp
VCHI initialization failed
However, I should have read the thread again. Now I understand the ping.cgi script (I thought it was some sort of script that was trying to ping a host ... silly me).

Anyway, I tried the ping.cgi script myself. It turn out that it I put the script in /var/www my browser wants to download the script. That is lighttpd doesn't execute it. However if I move the script to /var/www/cgi-bin/ it works.

Re: Run terminal command from webpage

Posted: Sat Nov 17, 2012 12:05 pm
by cazz
ok but I don't think it is that problem.

I have even try "Hello world" script and nothing happend.

It does not say anything, no error, no download just a plain white page.

The only thing I have add in my conf file is so I can run PHP.
I wonder if that is the problem

Code: Select all

server.modules = (
	"mod_access",
	"mod_alias",
	"mod_compress",
	"mod_fastcgi",
 	"mod_redirect",
#       "mod_rewrite",
)

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".cgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

  fastcgi.server = ( ".php" => ((
                     "bin-path" => "/usr/bin/php-cgi",
                     "socket" => "/tmp/php.socket"
                 )))

Re: Run terminal command from webpage

Posted: Tue Nov 26, 2013 11:34 am
by Honsolgen
SetUID on vcgencmd and it works ;)

chmod u+s /opt/vc/bin/vcgencmd


Image

Image

Image

Re: Run terminal command from webpage

Posted: Tue Nov 26, 2013 12:33 pm
by vadim
check out RaspCtl

Re: Run terminal command from webpage

Posted: Mon Apr 11, 2016 7:37 am
by arpan9818
hi .. i am trying to run this command on a web page but i got a blank page .

Code: Select all

sudo /home/pi/sources/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 4
[/color][/color]

i have attached the image which shows when in run above command in terminal of my raspberry pi.