One of the things I like to do is to make a more useful interface for my computers. For example, running command line programs from a web browser. Normally one might think this would unusual, but with the Common Gateway Interface (CGI) via the Apache web server makes it very possible. I love this for touch screen devices where tying is less of an option (i.e. touchpads). It not so powerful as yet to have an on-screen program per se. but you can use textual interfaces to get what you want. I am starting to add more and more commands to a page like this:

Submit the option you want and you get this.

Though this might be an over simplification of what you can do it certainly peeks the imagination. You could even add code to display memory via free or hard disk usage as a whole with df -h. All kinds of options. Here is the code as shown.

Bash1.cgi (sudo chmod +x and put into your cgi directory)

#!/bin/sh
echo "Content-type: text/htmln"

# 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"`

# our html header
echo "<html>"
echo "<head><title>Bash1 CGI</title></head>"
echo "<body>"

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

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

    dmesg)
      echo "Output of dmesg :<pre>"
      /bin/dmesg
      echo "</pre>"
      ;;

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

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

    *)
      echo "Unknown command $CMD<br>"
      ;;
  esac
fi

# print out the form

# page header
echo "<p>"
echo "<center>"
echo "<h2>Bash commands</h2>"
echo "</center>"
echo "<p>"
echo "<p>"

echo "Choose which command you want to run"
echo "<form method=get>"
echo "<input type=radio name=cmd value=ifconfig checked> ifconfig <br>"
echo "<input type=radio name=cmd value=uname> uname -a <br>"
echo "<input type=radio name=cmd value=dmesg> dmesg <br>"
echo "<input type=radio name=cmd value=lsb_release> lsb_release <br>"
echo "<input type=radio name=cmd value=ls> ls  -- folder <input type=text name=folder value=/mnt/flash><br>"
echo "<input type=submit>"
echo "</form>"
echo "</body>"
echo "</html>"

To add a command is simple as adding an option in the case section and adding a radio option at the bottom of the code. For example here is the “df -h” and the “free” command added. You can pretty much cut and paste with simple modifications.

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

free)
      echo "Output of free :<pre>"
      /usr/bin/free
      echo "</pre>"
      ;;

and

echo "<input type=radio name=cmd value=df> df -h <br>"
echo "<input type=radio name=cmd value=free> free <br>"

The new web page:

Bash1.cgi (updated)

#!/bin/sh
echo "Content-type: text/htmln"

# 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"`

# our html header
echo "<html>"
echo "<head><title>Bash CGI</title></head>"
echo "<body>"

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

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

    dmesg)
      echo "Output of dmesg :<pre>"
      /bin/dmesg
      echo "</pre>"
      ;;

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

free)
      echo "Output of free :<pre>"
      /usr/bin/free
      echo "</pre>"
      ;;

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

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

    *)
      echo "Unknown command $CMD<br>"
      ;;
  esac
fi

# print out the form

# page header
echo "<p>"
echo "<center>"
echo "<h2>Bash commands</h2>"
echo "</center>"
echo "<p>"
echo "<p>"

echo "Choose which command you want to run"
echo "<form method=get>"
echo "<input type=radio name=cmd value=ifconfig checked> ifconfig <br>"
echo "<input type=radio name=cmd value=uname> uname -a <br>"
echo "<input type=radio name=cmd value=dmesg> dmesg <br>"
echo "<input type=radio name=cmd value=lsb_release> lsb_release <br>"
echo "<input type=radio name=cmd value=df> df -h <br>"
echo "<input type=radio name=cmd value=free> free <br>"
echo "<input type=radio name=cmd value=ls> ls  -- folder <input type=text name=folder value=/mnt/flash><br>"
echo "<input type=submit>"
echo "</form>"
echo "</body>"
echo "</html>"

One thing to remember is that we did not have to start from scratch to update the options. Also you want to keep this web page hidden so that unwanted eyes do not take advantage of it. As you saw we pretty much just cut and paste. Again to to express the usefulness. I can check on the web server disk space before I try to upload a large file. All this was done with just a simple text editor via ssh. Future additions will add the commands to access other servers/computers for their info. One write and many click.

Update:

lsuser (chmod +x and then goes into /usr/bin/)

#=================================
# list users
#=================================
# Assignments
# --------------------------------
datafile="/etc/passwd"
# end assignments

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

USE AT YOUR OWN RISK!

bash.cgi (goes into the cgi directory)

#!/bin/sh
	echo "Content-type: text/htmln"

	# 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 "<html>"
	echo "<head><title>Bash CGI</title></head>"
	echo "<body>"

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

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

	    dmesg)
	      echo "Output of dmesg :<pre>"
	      /bin/dmesg
	      echo "</pre>"
	      ;;

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

	   free)
	      echo "Output of free :<pre>"
	      /usr/bin/free
	      echo "</pre>"
	      ;;

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

 	    lsusb)
              echo "lsusb :<pre>"
              /usr/bin/lsusb
              echo "</pre>"
              ;;

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

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

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

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

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

           cpuinfo)
              echo "Cpu information :<pre>"
              cat /proc/cpuinfo
              echo "</pre>"
              ;;

	     *)
	      echo "Unknown command $CMD<br>"
	      ;;
	  esac
	fi

	# print out the form

	# page header
	echo "<p>"
	echo "<center>"
	echo "<h2>Bash commands</h2>"
	echo "</center>"
	echo "<p>"
	echo "<p>"

	echo "Choose which command you want to run"
	echo "<form method=get>"
	echo "<input type=radio name=cmd value=ifconfig checked> ifconfig (Network configuration) <br>"
	echo "<input type=radio name=cmd value=uname> uname -a (Kernel version)<br>"
	echo "<input type=radio name=cmd value=dmesg> dmesg (System messages) <br>"
	echo "<input type=radio name=cmd value=lsb_release> lsb_release (Ubuntu version) <br>"
	echo "<input type=radio name=cmd value=df> df -h (Free disk space) <br>"
	echo "<input type=radio name=cmd value=free> free (Memory info)<br>"
        echo "<input type=radio name=cmd value=cpuinfo> Cpu information <br>"
        echo "<input type=radio name=cmd value=hw> Hardware listing <br>"
	echo "<input type=radio name=cmd value=lsuser> User listing <br>"
	echo "<input type=radio name=cmd value=lsusb> lsusb (Usb ports info)<br>"
	echo "<input type=radio name=cmd value=ls> ls  -- folder <input type=text name=folder value=/mnt/flash><br>"
	echo "<input type=radio name=cmd value=lsal> ls -al -- folder <input type=text name=folder1 value=/mnt/flash><br>"
        echo "<input type=radio name=cmd value=wol> wakeonlan (enter mac address) <input type=text name=folder2 value=00:00:00:00:00:00><br>"
	echo "<input type=submit>"
	echo "</form>"
	echo "</body>"
	echo "</html>"

Final pic:

Good day!

Advertisements