Greetings,
URL parameters inside a http request are passed through the (shell)
environment. You will need to parse QUERY_STRING.
Here is a minimal shell CGI script :
----
#!/bin/sh
echo 'Content-type: text/plain'
echo
env
----
Here is a sample result :
SERVER_SIGNATURE=<address>Apache Server at 127.0.0.1 Port 80</address>
HTTP_KEEP_ALIVE=300
HTTP_USER_AGENT=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)
Gecko/20051007 Galeon/2.0.0 (Debian package 2.0.0-1)
SERVER_PORT=80
HTTP_HOST=127.0.0.1
DOCUMENT_ROOT=/home/matt/var/www
HTTP_ACCEPT_CHARSET=ISO-8859-1,utf-8;q=0.7,*;q=0.7
SCRIPT_FILENAME=/home/matt/var/cgi-bin/test-cgi.sh
REQUEST_URI=/cgi-bin/test-cgi.sh?foo=123&bar=lots%20of%20beers
SCRIPT_NAME=/cgi-bin/test-cgi.sh
HTTP_CONNECTION=keep-alive
REMOTE_PORT=37711
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/matt/var/cgi-bin
SERVER_ADMIN=
HTTP_ACCEPT=text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
REMOTE_ADDR=127.0.0.1
SHLVL=1
SERVER_NAME=127.0.0.1
SERVER_SOFTWARE=Apache
QUERY_STRING=foo=123&bar=lots%20of%20beers
SERVER_ADDR=127.0.0.1
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
HTTP_ACCEPT_ENCODING=gzip,deflate
REQUEST_METHOD=GET
_=/usr/bin/env
For your need, I suggest you :
----
#!/bin/sh
echo 'Content-type: text/plain'
echo
CMD=`echo "$QUERY_STRING" | grep -oE "(^|[?&])cmd=[^&]+" | sed "s/%20/
/g" | cut -f 2 -d "="`
echo "Read[$CMD]"
$CMD
---
If for exemple I try
"http://127.0.0.1/cgi-bin/test-cgi.sh?cmd=ls%20-ail&&foo=bar", I get:
Read[ls -ail]
total 28
688187 drwxrwxrwx 2 matt matt 4096 Jan 15 11:59 .
34137 drwxr-xr-x 4 matt matt 4096 Jan 15 11:09 ..
688189 -rw-r--r-- 1 matt matt 12288 Jan 15 11:59 .test-cgi.sh.swp
688188 -rwxrwxrwx 1 matt matt 164 Jan 15 11:59 test-cgi.sh
If you want to create more complicated scripts, you should use perl (or
python, ...)
There are some nice perl modules which parses the QUERY_STRING for you
(see cpan.org).
A "use CGI qw(:standard)" will save you lots of time! There is also an
well-known-old-famous "cgi-bin.pl" lib.
Matthieu
Andy Gryc wrote:
>Just make your CGI a script like this:
>$1 $2 $3 $4 $5 $6 $7
>
>I don't have a shel reference handy, but I think $* may work for all
>args.
>--Andy Gryc
>
>On Thu, 12 Jan 2006 2:24 pm, chentom60 wrote:
>
>
>>Thanks Jim,
>>
>>I know how CGI gets parameters from browser. But do you know how can
>>CGI execute this parameter as a shell command? e.g. if my CGI get
>>such an parameter "ls -l" from browser, how do I let it excute ls -l?
>>
>>
>>Tom
>>
>>--- In Jim Jackson <> wrote:
>>
>>
>>>
>>>
>>> On Thu, 12 Jan 2006, chentom60 wrote:
>>>
>>> > Hi,
>>> >
>>> > I have a TS-7250 and have put some executibles to change DIO Port
>>>
>>>
>>pin
>>
>>
>>> > High/Low, Turn Red/Green LED on/off etc.
>>> >
>>> > I am wondering how can I call these functions or shell commands on
>>> > board from a PC web browser. I wrote a html file which can send
>>>
>>>
>>request
>>
>>
>>> > to TS7250 web server and wrote a CGI script which can call any one
>>> > function on board. It is successful, however, it seems that CGI
>>>
>>>
>>script
>>
>>
>>> > only runs functions embedded in the script. Is it possible that
>>>
>>>
>>CGI
>>
>>
>>> > executes any dynamic requests from user? So, users can do some
>>>
>>>
>>tests or
>>
>>
>>> > check board status from PC?
>>> >
>>> > I am thinking a way(not limited to CGI, any method is ok) for
>>>
>>>
>>users to
>>
>>
>>> > send requests from web browser(type any function name or just
>>>
>>>
>>shell
>>
>>
>>> > command on a text box and press a button to send it to the
>>>
>>>
>>board), then
>>
>>
>>> > 7250 executes it and sends information back to browser? Anyone
>>>
>>>
>>can help
>>
>>
>>> > me?
>>>
>>> You can pass paramters to a web page by appending "?" and the
>>>
>>>
>>parameters
>>
>>
>>> to the end of the cgi-bin URL. If you make a cgi script that
>>>
>>>
>>executes the
>>
>>
>>> parameter given then I think you have what you want.
>>> see http://www.unix.org.ua/orelly/perl/learn/ch19_04.htm
>>>
>>> You also have an ENORMOUS security hole - make sure you are on
>>>
>>>
>>a "safe"
>>
>>
>>> network. see http://www.w3.org/Security/Faq/wwwsf4.html
>>>
>>> cheers
>>> Jim
>>>
>>>
>>>
>>
>>
>>
>>
>>
>>
>>Yahoo! Groups Links
>>
>>
>>
>>
>>
>--Andy Gryc
>
>http://gryc.ws
>
>
>
>Yahoo! Groups Links
>
>
>
>
>
>
>
>
>
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|