Simple Linux Commands From a Web Page.


Introduction: Simple Linux Commands From a Web Page.

About: 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

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:

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

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.

# lsuser - list users
# Assignments
# --------------------------------
# end assignments

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

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

    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 ]
      case "$CMD" in
          echo "Output of ifconfig :<pre>"
          echo "</pre>"
          echo "Output of uname -a :<pre>"
          /bin/uname -a
          echo "</pre>"
          echo "Output of dmesg :<pre>"
          echo "</pre>"
          echo "Output of df -h :<pre>"
          /bin/df -h
          echo "</pre>"
          echo "Output of free :<pre>"
          echo "</pre>"
              echo "Hardware listing :<pre>"
              echo "</pre>"

              echo "lsusb :<pre>"
              echo "</pre>"

              echo "List of users :<pre>"
              echo "</pre>"

          echo "Output of ls $FOLDER :<pre>"
          /bin/ls "$FOLDER"
          echo "</pre>"
              echo "Output of ls $FOLDER1 :<pre>"
              /bin/ls -al "$FOLDER1"
              echo "</pre>"

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

          echo "Ubuntu version :<pre>"
          /usr/bin/lsb_release -a
          echo "</pre>"
              echo "Cpu information :<pre>"
              cat /proc/cpuinfo
              echo "</pre>"
          echo "Unknown command $CMD<br>"
    # 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>"

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.

              echo "Output of free :<pre>"
              echo "</pre>"

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

              echo "Who is logged in :<pre>"
              echo "who      where        login-time   logged-in-from"
              echo "---------------------------------------------------"
              echo "</pre>"

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

Now we need to copy an option:

echo "<input type=radio name=cmd value=lsuser> User listing <br>"

Then edit it for our needs

echo "<input type=radio name=cmd value=who> Who is logged in <br>"

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

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

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.

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

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 ( with the bash scripting to put it all together so you do not have to remember all the commands. It's here:

Step 9: Updated Screen Shots.

Just some updated screenshots.



    • Oil Contest

      Oil Contest
    • Woodworking Contest

      Woodworking Contest
    • Make it Move Contest

      Make it Move Contest

    We have a be nice policy.
    Please be positive and constructive.


    2 Questions

    i used cat command on log files it dosent work i found the problem

    that is log file has no permission to others for read

    can you tell me that how i run this command by group or root so i can open the log file without changing permission

    Exceptional effort ... I have one question ... How can you take an input from HTML text box and execute it as a Linux command. example -- changing folder directories..

    echo " ls -al -- folder


    Hello is that possible to run adduser command?

    echo "<input type=radio name=cmd value=createuser> create user account <input type=text name=username value=Enter_your_username><br>"


    echo "Hey $USERNAME, your username is set<pre>"

    /home/sudo adduser "$USERNAME"

    echo "</pre>"


    I have did this but my output is just displaying "Hey someusername, your username is set" but its actually doesn't set

    good post, thanks for sharing with us. we too working on Linux for implementing new projects. We frequently browse internet for hot topics in Linux. We found this post good.

    1 reply

    Followed all instructions to the letter, when I place "bash.cgi" into /var/www/cgi-bin and try to access it via I get Internal server Error 500. 0.o I'm on Fedora 20 with the httpd package from YUM.

    5 replies

    Ok I fixed this issue, now instead, when I go to the address, it tried to download the bash.cgi file, I think it's an error in the script itself, because when I try just "
    echo "Content-type: text/html\n"
    echo "Testing..."
    it works fine.

    Fixed this one on my own again.
    Change the very first echo line to
    echo -e "Content-type: text/html\n"

    Worked like a charm.

    You have to set up a directory specifically for doing the cgi bin and tell the web server configuration about it. The normal cgi-bin is not normally in /var/www. With apache 2.4 configuration files have changed, I have not had a chance to look at it yet, but a lot of things have changed.By the way I do not use the localhost address, I use the normal ipaddress.

    In /etc/apache2/sites-available/000-default.conf

    #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    # <Directory "/usr/lib/cgi-bin">

    <Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

    Can use this for openwrt? I can control GPIO of router via ssh command, but i can not create web interface.

    1 reply

    openwrt and luci has a cgi bin dir. You will need to install luci if you have not already done so. Embed your gpio command in the cgi script.

    There must be a blank line between the HTTP header and the body. Also, you might want to serve up a complete html document.

    echo "Content-type: text/html"
    echo "<html><head><title>hello world</title></head><body><p>It works!</p></body>"

    Can use this for openwrt? I can control GPIO of router via ssh command, but i can not create web interface.

    I don't have access to /var/www/cgi-bin/ when I try to access it through my browser and when I place the file elsewhere, it displays it as plain text. How can I fix this?

    2 replies

    I know I responded to this. replay was missing. You need to make sure that the .txt extention is not used. secondly, you need to make sure the cgi is set up right. Do not know your setup, so it is hard to suggest anything. Also check your permissions.

    -rwxr-xr-x 1 www-data www-data 3729 Jun 2 14:53 bashcgi1.cgi

    Do not know which server you are using. You will want to go and check your cgi is installed correctly with correct permissions. Hint: look at the apache.conf file.