Executing Linux / UNIX commands from web page

by nixcraft · 26 comments

A Web interfaces is almost used by routers and many other sophisticated programs such as webmin. However, why go for a web interface or execute commands from web page? For automation purpose, you need to use a web interfaces. Another advantage is you can access your web-based interface from any computer, running any operating system, anytime in the world :D

In this first part, you will see how to use simple bash (shell) script from web page. In order to execute commands or shell script from a webpage you need:

  1. CGI support with Apache / lighttpd web server.
  2. I'm assuming that you have a properly configured web server.

You need to store program in cgi-bin directory. If you are using Debian Linux default location for cgi-bin directory is /usr/lib/cgi-bin. Under Red Hat / Fedora it is /var/www/cgi-bin. Use text editor such as vi to create a first.cgi program:

$ cd /usr/lib/cgi-bin
$ vi first.cgi

first.cgi code listing:

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>Hello world</h1>"
echo "Today is $(date)"

echo "</body></html>"

Save and close the file. Setup execute permission on the script:

$ chmod +x first.cgi

Fire up your web browser and test the script, for example type url http://localhost/cgi-bin/first.cgi or http://your-ip/cgi-bin/first.cgi

You need to send headers, first three lines are almost same for all your script:

  • #!/bin/bash : First line tell Linux/UNIX how file first.cgi should be run. So it will use /bin/bash interpreter to execute your rest of program.
  • echo "Content-type: text/html" : Send html headers, you must include this line.
  • echo "" : Send a blank line, you must include this line.

Rest is html code. Take a close look at following echo command:

echo "Today is $(date)"

It use shell feature called command substitution. It allows the output of a command to replace the command name:

$(command)

Your bash shell performs the expansion by executing command and replacing the command substitution. So date command get executed by replacing the its output.

Real life example

Here is simple script that collects system information. Create script in cgi-bin directory:

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>General system information for host $(hostname -s)</h1>"
echo ""

echo "<h1>Memory Info</h1>"
echo "<pre> $(free -m) </pre>"

echo "<h1>Disk Info:</h1>"
echo "<pre> $(df -h) </pre>"

echo "<h1>Logged in user</h1>"
echo "<pre> $(w) </pre>"

echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Save and close the file. Setup execute permission on script:

$ chmod +x script.cgi

Fire up web browser and test it (http://localhost/cgi-bin/script.cgi):

Next time you will see:

  • How to use and place form elements (from POSTs and GETs)
  • Cookies in your environment
  • Use of perl scripting
  • And finally use of special tools

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 26 comments… read them below or add one }

1 Randal L. Schwartz 01.06.06 at 12:25 am

You need to HTML-escape (add ampersands) your command output, or you might be accidentally providing a means for cross-site-scripting attacks.

2 nixcraft 01.07.06 at 12:44 pm

Randal, thanks, I will cover this later in security issues :) I appreciate your post.

3 007ben 01.18.06 at 8:42 am

I am struggling with creating a empty file a bash based cgi script. the filename should coome from information passed to the cgi from a forum.
help?

4 nixcraft 01.18.06 at 9:37 am

007ben, I hope you have write permission to create a empty file.

If you just want to retrieve value pass to url for example http://mydom.com/cgi-bin/script.cgi?filename

Then use $1 in script to get filename i.e.
touch $1

5 nixcraft 01.18.06 at 9:49 am

So if you are using url
http://mydom.com/cgi-bin/script.cgi?file=/tmp/abc.txt
then following shell code should find out value of file:

LINE=`echo $QUERY_STRING | sed ’s/&/ /g’`
for i in $LINE
do
f1=$(echo $i | cut -d= -f1)
v1=$(echo $i | cut -d= -f2)
done
echo “Var is f1 and value is $v1″

6 007ben 01.18.06 at 11:00 am

that last bit was the trick!

the forum was not passing the info with variable name.
and when it did before the script could not pick it up.

THANK YOU!

7 Anonymous 02.21.06 at 7:11 pm

hello sir.,
I am doing my project in linux.I want to know how to execute useradd and iptables commands in linux from PHP.How can i use the form data as arguments to the program.Please give the solution.
S.Sujaikumar

8 nixcraft 02.23.06 at 4:09 pm

Use system(), exec() commands. For example to execute ls -l command you can write code:


$out = system(“ls -l”);
echo $out;

Suppose you are using url http://localhost/page.php?user=raj

Then you can use following code to get value of user:
$u = $_GET['user'];
$cmd = “adduser “. $u;
$out = system($cmd);

9 Anonymous 08.24.06 at 7:03 am

I m doing my project in my university network, which means that I m not the root. Hence I dont have any writtin permissions in cgi-bin directory. Is ther any other way out? I wud be greatfull if you can lemme knw the solution.
thanks a lot
vasanth

10 Jadu 11.15.07 at 9:21 am

nixcraft is always best :-) Thanks, I am used to BASH, and your helps are always good for me. Is there any doc which tells more about cgi and bash together? like accepting user inputs, database access and all, will be a great help for me.

Thanks,
Jadu, India

11 vivek 11.15.07 at 12:48 pm

Jadu,

Although bash is handy, it is recommended that you use python or perl for cgi and sys admin related task.

HTH

12 olivier jolly 01.09.08 at 11:57 am

Hi,
I know this topic is a bit old, but I think it could be useful to alter the comment of nixcraft to be like this :

#!/bin/bash

echo "Content-type: text/plain"
echo

LINE=`echo $QUERY_STRING | sed 's/&/ /g'`
for i in $LINE
do
f1=$(echo $i | cut -d= -f1)
v1=$(echo $i | cut -d= -f2)
typeset -r $f1=$v1
done

echo "hard coded content of t = '$t'"

Like this, you are generating shell variables that you can use when you know in advance their name (as shown in the last line). It still lacks post support and decoding of escaped character but for simple script it can useful.
hth

13 Ravi 06.12.08 at 4:51 am

Hello,

i want to run a “tail -f ” command using cgi to view unix log files progress in real time from browser.

any ideas? or cgi script can do it?

14 Thor Henrik 06.13.08 at 12:35 pm

Hi

I have writen a bash script for work that accesses sevral servers and collect key information about them and creates a web page with the info.

I would like to be able to run the shell script from the web page.
Just to be able to do ./script.sh $1
Is this possible

15 kwissem 06.16.08 at 11:02 pm

I want to open konsole and exute telnet 10.10.10.1 and the konsole be opened from web page

16 Princess 07.10.08 at 11:14 am

Hi

I have a bash script here that will create email accounts for each person that will request for 1.

I want this script to be web based is this possible?

Anyone can help me…

Thanks

17 Ratnesh Bajpai 02.11.09 at 11:24 am

Hello i want to know the way to creating a directory in linux through browser execution using php. thnx in advance

18 mailmado 05.02.09 at 5:51 am

hello
i want to run a simple mkdir command in cgi script which will take directory name from html page
pl help.

19 Pex 05.14.09 at 9:13 am

Hello,
I want to install web server with php support. what are the steps to be taken??
This is on Linux!!

Pliz help!!

20 narf 10.02.09 at 12:36 am

I get this on the /var/log/httpd/error.log
Syntax error in type map, no ‘:’ in /var/www/cgi-bin/script.cgi for header “”\n

21 apctr 11.30.09 at 8:26 am

i want to execute vncviewer in web page.what should I have to do????????

22 berot3 12.12.09 at 7:59 pm

does it work on mac os x?

23 Vivek Gite 12.13.09 at 8:03 am

Yes, it should work under Mac OS X too

24 berot3 12.14.09 at 4:35 pm

yepp works on mac, just the path of the cgi is different: /Library/Webserver/CGI-Executables/
but thats not what i was looking for :)
maybe u can help me / maybe have heard of someting like this:
i want to execute commands as if i would be a ssh-console but for http, u know what i mean?

25 Vivek Gite 12.14.09 at 4:52 pm

This is going little offtopic. I suggest you post it to our forum along with shell code if you have any and will continue discussion at our forum http://nixcraft.com/

26 Guntur 01.05.10 at 11:55 am

hi, my server is using CentOS.. where to put the cgi?

I don’t understand anything bout cgi..
but I want to execute a bash command from web page..
like mkdir or unzip etc..

please help me :)

thx

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: