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:
- CGI support with Apache / lighttpd web server.
- 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:
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
- 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
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop