Simple Linux Commands From a Web Page.

74K2323

Intro: 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 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.

20 Comments

Can i use this to chanfge Network configuration on RPI 3b+. Like Wlan, Lan, SSID, SSID_pwd, dynamic dhcp or static ip address, net mask and etc.?
I did not get correctly how to direct the web server to the bash document which is created.
please help asap
I installed wakeonlan package. Does it automatically gets enabled?
If no then how to check. Im new to linux programming

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

createuser)

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.

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

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 "
#!/bin/sh
echo "Content-type: text/html\n"
echo
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
</Directory>

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

https://forum.openwrt.org/viewtopic.php?id=54555

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.

#!/bin/sh 
echo "Content-type: text/html"
echo
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.

https://forum.openwrt.org/viewtopic.php?id=54555

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?

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.