Introduction: Simple Linux Commands From a Web Page.

About: computoman.blogspot.com Bytesize articles instead of a trilogy in one post.

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 https://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: https://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.

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/.

Step 3: Adding Wakeonlan

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.

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 "<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>"
[/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.

Step 5: Testing.

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.

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 :<pre>"
              /usr/bin/free
              echo "</pre>"
              ;;
[/code]

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

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

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

Now we need to copy an option:

[code]
echo "<input type=radio name=cmd value=lsuser> User listing <br>"
[/code]

Then edit it for our needs

[code]
echo "<input type=radio name=cmd value=who> Who is logged in <br>"
[/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!!

Step 7: New Header.

Added/changed a few lines to personalise the header.

[code]
# page header
        echo "<p>"
        echo "<center><h2>"
        echo "Management Console for: "
        uname -n
        echo "</center></h2>"
        echo "<p>"
        echo "<p>"
        echo "<form method=get>"
        echo "Choose which command you want to run: <br>"
        echo "<br>System information<br>"
[/code]

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

Step 8: Coming Soon.

We are going to combine  the web page scraping (https://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: https://www.instructables.com/id/Web-page-scraping-fromto-a-web-page/

Step 9: Updated Screen Shots.

Just some updated screenshots.