ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How Can you run Linux commands and view its output in the web browser?

user-image
تم إضافة السؤال من قبل Mostafa Mohamed Abdelrahman Mohamed , Senior Virtualization and Application Management Executive , Qatar National Bank - ALAHLI
تاريخ النشر: 2015/05/13
Muhammad Anzar
من قبل Muhammad Anzar , DevOps/DevSecOps Architect , Confidential

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

مستخدم محذوف‎
من قبل مستخدم محذوف‎

Enable CGI along with PHP in your linux box will give you leverage to execute commands in web browser and display output. 

 

 

 

 

المزيد من الأسئلة المماثلة