Simple linux commands from a web page.
Simple linux commands from a web page.

There had to be a way to do simple commands for linux without going to the command line. The neat part to this project is that you can so easily expand it yourself. Need a directory listing. No problem. Need to know how the disk space is doing. No problem. This simple project will probably expand to a whole management system eventually. But in the mean time, here is just the basic structure which you can easily expand on. Also see http://www.instructables.com/id/Web-page-scraping-fromto-a-web-page/

Note: if you are on a terminal that does not have a terminal client, this project is a real lifesaver. This project makes it real easy for me to use a touchpad and check on my servers. No special client software necessary. Also this could be very easily adapted for the Apple Macintosh systems also since it is allegedly bsd based (a cousin of linux). Yes, control your "Mac" from a web page.

If you like this instructable, you might like this one also: http://www.instructables.com/id/Linux-screen-play/

Installed Arch linux on a pogoplug v2 and that is a wee bit different to set up the cgi-bin.

Step 1: What's needed:

Human with a basic understanding of BASH (Bourne again shell).
http://www.freeos.com/guides/lsst/

Computer running Apache or other web server that supports the CGI (Common gateway interface).
A Lamp (linux, apache, mysql, and php) server would be a good start.

Step 2: Extra program needed.

Simple linux commands from a web page.
We need to create a simple program to list users for use with the system. So you need to create a file called lsuser with a text editor such as nano, vim, or whatever.

[code]
#=================================
# lsuser - list users
#=================================
# Assignments
# --------------------------------
datafile="/etc/passwd"
# end assignments

#=================================
#
# Data input
#---------------------------------
while read line
do echo $line | cut -d: -f1
done < $datafile
[/code]

Once the file is created we need to make it executable.

$ chmod +x lsuser

The we need to copy it to the usr program directory.

$ sudo cp lsuser /usr/bin/.

Simple linux commands from a web page.
lsuser.txt272 bytes

Step 3: Adding wakeonlan

Simple linux commands from a web page.
wakeonlan is a special program that will send info to wake up a computer that has been set for wakeonlan. that is you can turn on computers remotely. You can use the command line or your favorite package manager. Machines to be awaken will have to be set up for this. Usually it is in the bios setup.

$ sudo apt-get install wakeonlan

Step 4: The main program.

Simple linux commands from a web page.
You will need to copy the code into a file called bash.cgi. The file extention must be ".cgi".

bash.cgi
[code]
i#!/bin/sh
echo "Content-type: text/html\n"

# read in our parameters
CMD=`echo "$QUERY_STRING" | sed -n 's/^.*cmd=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
FOLDER=`echo "$QUERY_STRING" | sed -n 's/^.*folder=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"| sed "s/%2F/\//g"`
FOLDER1=`echo "$QUERY_STRING" | sed -n 's/^.*folder1=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"| sed "s/%2F/\//g"`
FOLDER2=`echo "$QUERY_STRING" | sed -n 's/^.*folder2=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"| sed "s/%2F/\//g"`

# our html header
echo ""
echo "Bash CGI"
echo ""

# test if any parameters were passed
if [ $CMD ]
then
case "$CMD" in
ifconfig)
echo "Output of ifconfig :"
/sbin/ifconfig
echo ""
;;

uname)
echo "Output of uname -a :"
/bin/uname -a
echo ""
;;

dmesg)
echo "Output of dmesg :"
/bin/dmesg
echo ""
;;

df)
echo "Output of df -h :"
/bin/df -h
echo ""
;;

free)
echo "Output of free :"
/usr/bin/free
echo ""
;;

hw)
echo "Hardware listing :"
/usr/bin/lshw
echo ""
;;


lsusb)
echo "lsusb :"
/usr/bin/lsusb
echo ""
;;

lsuser)
echo "List of users :"
/usr/bin/lsuser
echo ""
;;

ls)
echo "Output of ls $FOLDER :"
/bin/ls "$FOLDER"
echo ""
;;

lsal)
echo "Output of ls $FOLDER1 :"
/bin/ls -al "$FOLDER1"
echo ""
;;

wol)
echo "System to wake: $FOLDER2 :"
/usr/bin/wakeonlan "$FOLDER2"
echo ""
;;


lsb_release)
echo "Ubuntu version :"
/usr/bin/lsb_release -a
echo ""
;;

cpuinfo)
echo "Cpu information :"
cat /proc/cpuinfo
echo ""
;;

*)
echo "Unknown command $CMD
"
;;
esac
fi

# print out the form

# page header
echo "

"
echo ""
echo "

Bash commands

"
echo ""
echo "

"
echo "

"

echo "Choose which command you want to run"
echo ""
echo " ifconfig (Network configuration)
"
echo " uname -a (Kernel version)
"
echo " dmesg (System messages)
"
echo " lsb_release (Ubuntu version)
"
echo " df -h (Free disk space)
"
echo " free (Memory info)
"
echo " Cpu information
"
echo " Hardware listing
"
echo " User listing
"
echo " lsusb (Usb ports info)
"
echo " ls -- folder
"
echo " ls -al -- folder
"
echo " wakeonlan (enter mac address)
"
echo ""
echo ""
echo ""
echo ""
[/code]

then you will need to make it executable.

$ chmod +x bash.cgi

The you must copy it into your cgi directory. (check to make sure there is not already a file of the same name!).

$ sudo cp bash.cgi /var/www/cgi-bin/.

You should be ready to test it now.

Simple linux commands from a web page.
bashcgi1.cgi.txt3 KB

Step 5: Testing.

Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Direct your web server to where the bash.cgi (I called bashcgi1.cgi for my purposes.) Use the mouse to click on the tool you want to use. Viola and you have instant command line control of a server at the click of the mouse. This beats logging into a server via the command line and typing things in. One write and many click! Your own free tool!

Step 6: Adding a feature.

Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
You nay want to add a new feature. it is simple as cutting and pasting.

Take a piece of code from the command section and copy it.

[code]
free)
echo "Output of free :"
/usr/bin/free
echo ""
;;
[/code]

Then edit it. (I added a header to make it more readable.)

[code]
who)
echo "Who is logged in :"
echo "who where login-time logged-in-from"
echo "---------------------------------------------------"
/usr/bin/who
echo ""
;;
[/code]

Add it to the code if you have not done so already.

Now we need to copy an option:

[code]
echo " User listing
"
[/code]

Then edit it for our needs

[code]
echo " Who is logged in
"
[/code]

Again add it back in if you have not done so already.

If you want to do input you will need to add another section also. (you need to change FOLDER2/folder2 names).

[code]
FOLDER3=`echo "$QUERY_STRING" | sed -n 's/^.*folder3=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"| sed "s/%2F/\//g"`
[/code]

Note you may want to fancy up your menu also to make it more user friendly. (see attached file for details).

Have Fun!!

Simple linux commands from a web page.
bashcgi1.cgi.txt4 KB

Step 7: New Header.

Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Added/changed a few lines to personalise the header.

[code]
# page header
echo "

"
echo "

"
echo "Management Console for: "
uname -n
echo "

"
echo "

"
echo "

"
echo ""
echo "Choose which command you want to run:
"
echo "
System information
"
[/code]

Also included is the original and then a more up to date view of the program. (source code not included.) Quite a change.

Simple linux commands from a web page.
bashcgi1.cgi.txt4 KB

Step 8: Coming soon.

Simple linux commands from a web page.
We are going to combine the web page scraping (http://www.instructables.com/id/Web-page-scraping-via-Linux/) with the bash scripting to put it all together so you do not have to remember all the commands. It's here: http://www.instructables.com/id/Web-page-scraping-fromto-a-web-page/

Step 9: Updated screen shots.

Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Simple linux commands from a web page.
Just some updated screenshots.
 
 

Tag cloud

make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel