Simple Linux Commands From a Web Page.
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:
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.
[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
$ sudo apt-get install wakeonlan
STEP 4: The Main Program.
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.
STEP 6: Adding a Feature.
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.
[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.
20 Comments
za1996 3 years ago
raianu233 5 years ago
please help asap
raianu233 5 years ago
If no then how to check. Im new to linux programming
ShubhamT70 5 years ago
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
JamieB176 6 years ago
echo " ls -al -- folder
"
ArunkumarG5 6 years ago
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
MarketingT1 7 years ago
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.
Computothought 7 years ago
Thank you for your reply..
SpiritlessAngel 9 years ago
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.
SpiritlessAngel 9 years ago
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.
SpiritlessAngel 9 years ago
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.
Computothought 9 years ago
Interesting. never had to do that.
Computothought 9 years ago
My setup still works fine.
Computothought 9 years ago
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>
ChứcN 9 years ago
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
Computothought 9 years ago
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.
ChứcN 9 years ago
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
wdmc85 9 years ago
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?
Computothought 9 years ago
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
Computothought 9 years ago
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.