LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2010, 08:07 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
how to combine CGI script with a bash script


I have a fallowing CGI script, which does nothing more than prints out the values user inserted:

Code:
#!/bin/sh
echo "Content-type: text/html"
echo
ID=`echo "$QUERY_STRING" | sed -n 's/^.*id=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
TEXT=`echo "$QUERY_STRING" | sed -n 's/^.*text=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

echo "<html>"
echo "<head><title>SNMP script</title></head>"
echo "<body>"

if [ $ID ] && [ $TEXT ]
then
  echo "Name is $ID <br>"
  echo "IP address is $TEXT <br>"
else
  echo "<h4>SNMP script</h4>"
  echo "<form method=get>"
  echo "Name: <input type=text name=id><br>"
  echo "IP address: <input type=text name=text><br>"
  echo "<input type=submit value=Generate>"
  echo "</form>"
fi

echo "</body>"
echo "</html>"
If I insert Martin to the name box and 192.168.1.1 to the IP box I get fallowing output:
Quote:
Name is Martin
IP address is 192.168.1.1
As you can see, script above works fine(I host this script in Apache server and use Firefox as a web-client). Now I made a fallowing bash script:

Code:
#!/bin/bash
if (($# < 1))
then
echo "USAGE: $0 IP1 IP2 IP3 IP4 etc"
else
echo Generated by $1
echo
while [ "$2" != "" ]; do
paste <(snmpwalk -v 2c -c public $2 ifdescr | sed 's/.*STRING: //') <(snmpwalk -v 2c -c public $2 ifalias | sed 's/.*STRING: //')
shift
done
fi
...which is started like this: ./script.sh Martin 192.168.1.1. Output is user name plus port numbers and descriptions. This bash script works fine as well

How can I combine those two scripts? I would like to insert name and IP to according boxes in Firefox and after clicking Generate button, user name plus list of port numbers and descriptions will appear in Firefox window instead of terminal emulator window

How to link/combine those two scripts to work together as one?
 
Old 02-09-2010, 08:22 AM   #2
eyemole80
Member
 
Registered: Jun 2004
Distribution: Fedora Core 1
Posts: 47
Blog Entries: 1

Rep: Reputation: 16
#!/bin/sh
echo "Content-type: text/html"
echo
ID=`echo "$QUERY_STRING" | sed -n 's/^.*id=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
TEXT=`echo "$QUERY_STRING" | sed -n 's/^.*text=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

echo "<html>"
echo "<head><title>SNMP script</title></head>"
echo "<body>"

if [ $ID ] && [ $TEXT ]
then
echo "Name is $ID <br>"
echo "IP address is $TEXT <br>"
output=`paste <(snmpwalk -v 2c -c public $TEXT ifdescr | sed 's/.*STRING: //') <(snmpwalk -v 2c -c public $TEXT ifalias | sed 's/.*STRING: //')`
echo "$output <br>"
else
echo "<h4>SNMP script</h4>"
echo "<form method=get>"
echo "Name: <input type=text name=id><br>"
echo "IP address: <input type=text name=text><br>"
echo "<input type=submit value=Generate>"
echo "</form>"
fi

echo "</body>"
echo "</html>"

=========================
I think above should work, but I have not checked it. Also in bash cgi , you have to take care of encoding the forms data by yourself. if you are not doing that then a malicious user can exploit it. Be aware.
 
Old 02-09-2010, 09:39 AM   #3
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
eyemole80, thanks! However, this code doesn't quite work because it still just prints out name and IP, but nothing more. And am I correct, that logic behind CGI scripts is that all the bash code(for example loops, snmpwalks, sed and awk parts) parts should be defined as variables in the script(I mean like $TEXT, $ID, $output)?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash CGI script HTML tags issue m4rtin Programming 1 01-19-2010 12:57 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
cgi bash script paul_mat Linux - Software 4 04-20-2006 03:03 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration