أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.
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:
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.cgifirst.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.cgiFire 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:
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 exampleHere 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.cgiFire up web browser and test it (http://localhost/cgi-bin/script.cgi):Next time you will see:
Enable CGI along with PHP in your linux box will give you leverage to execute commands in web browser and display output.