≡ Menu

web interface

Razvan talk about hooking Amarok to MySQL database server and create an MP3 file server. I this is an excellent hack:

When it comes to playing music in Linux, Amarok is one of the best audio players out there. It offers almost everything you need, from a clean, intuitive interface to a range of useful scripts. You can even put it on a server and give it a Web interface.

ObsidianMusic was previously known as amaroK Web Frontend. It is a collection of scripts that, combined with a MySQL database and the Amarok player, forms an excellent way of sharing MP3 files over the Internet or a small office network. All the music files in your collection can be played, downloaded, or streamed by a regular Web browser. Furthermore, ObsidianMusic offers search capabilities and music sorting, and can be customized using themes.

=> Create an MP3 file server using Amarok and ObsidianMusic

Linux success story – Using Linux at Work

Yet another Linux success story, from the article:

I am by no stretch of the imagination a Linux expert, but my overall experience has been excellent and I shall continue to use Fedora for my day to day work. My productivity has not been affected at all, and anyone who wants to try something different, or take a cheaper OS route, should consider a look at Linux - it's really not that scary.

I've been programming since a young age, and Linux has always seemed like a natural progression, especially as my development environment is PHP/MySQL/Apache. A while ago, this was all done on a Red Hat installed system, using the "Plesk" web interface. Although I spent quite a few hours at the console sorting out problems, Plesk hid the real nitty gritty from me and I was often just following "How Tos" in order to get things fixed. In saying that, I did manage to write a wrapper script that fixed a compatibility between MailMan and Plesk, so I wasn't doing too badly. However, I would hardly say I felt confident in Linux, and using it for my day to day work seemed strangely frightening.

Read more, Using Linux at Work...

Linux Shell Script to reboot DSL or ADSL router

If you need to reboot the router then you need to use web interface or telnet interface. Both methods take time, especially if you are playing with ACL, NAT or router firewall or you just wanna reboot the router from your Linux desktop. I have created simple script using expect tool to reboot router. Make sure you have expect command installed. Use rpm or apt-get command to install expect tool.

Shell script

Create a script as follows (tested on Beetel ADSL 220x router):

#!/usr/bin/expect -f
 
set timeout 20
 
# router user name
set name "admin"
 
# router password
set pass "PASSWORD"
 
# router IP address
set routerip "192.168.1.254"
 
# Read command as arg to this script
set routercmd [lindex $argv 0]
 
# start telnet
spawn telnet $routerip
 
# send username & password
expect "Login:"
send -- "$name\r"
expect "Password:"
send -- "$pass\r"
 
# get out of ISP's  Stupid menu program, go to shell
expect " -> "
send --  "sh\r"
 
# execute command
expect "# "
send -- "$routercmd\r"
# exit
send -- "^D"

Save script and setup executable permission on it:
$ chmod +x router.exp

How do I run this script?

You need to pass command to script to execute on a router. For example to display router uptime, interface information and to reboot router you need to type command as follows:
$ ./router.exp uptime
$ ./router.exp ifconfig
$ ./router.exp reboot

Since my ISP router offers menu as soon as login above script may not work on generic router such as Cisco or linksys router. Therefore, you may need to modify above script to work with your router. If you are a new to expect then use autoexpect command to generate script. It watches you interacting with another program and creates an Expect script that reproduces your interactions For straightline scripts, autoexpect saves substantial time over writing scripts by hand. Even if you are an Expect expert, you will find it convenient to use autoexpect to automate the more mindless parts of interactions. It is much easier to cut/paste hunks of autoexpect scripts together than to write them from scratch. Moreover, if you are a beginner, you may be able to get away with learning nothing more about Expect than how to call autoexpect. Just type autoexecpt:
$ autoexpectautoexpect started, file is script.exp

Next type telnet command (telnet to the router):
$ telnet 192.168.1.254
Output:

Login: USER
Password: Password

Now type commands on the router:
$ ifconfig
$ exit

You are done, type exit to stop autoexepct command:
$ exit
Output:

autoexpect done, file is script.exp

Just type ./script.exp to run ifconfig command:
$ ./script.exp
You can now modify script.exp to reboot or to run other commands. It is a real lifesaver.

See also:

Executing Linux / UNIX commands from web page

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