Read query string in simple cgi program (/tmp/user/)

Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Generic Questions
Author Message
reflex
DD-WRT Novice


Joined: 07 Jun 2007
Posts: 20

PostPosted: Mon Oct 06, 2008 22:30    Post subject: Read query string in simple cgi program (/tmp/user/) Reply with quote
Hi,

I'm trying to write the simplest cgi program (to be placed in /tmp/user/cgi-bin), but I cannot seem to figure out how to receive an input parameter (read the query string)?

I would like to run this:
http://router_ip/user/cgi-bin/test.cgi?output=hello

And it should output to the browser: hello

So far my test.cgi just looks like this:
Code:
#!/bin/sh
QUERY_STRING = "$1"
echo $QUERY_STRING


I can run a normal cgi script (one that outputs `ps` for example) without any problems, I just can't access the query string or receive any inputs! I'm going crazy.. please help me!

Thanks,
David
Sponsor
reflex
DD-WRT Novice


Joined: 07 Jun 2007
Posts: 20

PostPosted: Wed Oct 08, 2008 12:48    Post subject: Reply with quote
Hmm.. no responses at all.

I suppose it's not possible then?

Any other way without installing perl/php or changing the web-server for another?

It's a shame, I'm really trying to write a script that will run a couple commands based on user input but I feel like thats impossible through http. :(

Thanks,
David
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 13:35    Post subject: Reply with quote
where did you find instructions on setting up a cgi script? where do I put it? I need to do almost the same.
reflex
DD-WRT Novice


Joined: 07 Jun 2007
Posts: 20

PostPosted: Thu Oct 23, 2008 15:10    Post subject: Reply with quote
1. create the folder /www/user/cgi-bin
which is really /tmp/user/cgi-bin

2. create a file test.sh (you can also use test.cgi)
in that folder:
3. do chmod +x test.sh
4. then edit the file with:
[code
#!/bin/sh

echo "hello world"
[/code]

Then when you figure out how to get an input from the query string or a form on the page reply to this topic!

If need be, you can also use .asp but its not the same ASP code I'm familiar with, its different.. but maybe it can be used to receive parameters from the query string and subsequently run commands on the router.

Let me know.
David
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 15:23    Post subject: Reply with quote
May be this would help?
http://www.unix.com/shell-programming-scripting/44291-parsing-url-string.html

The thing is I tried doing exactly the same - no success. It just doesn't run!
What firmware do you use? I'm running DD-WRT v23 SP3 mini
I believe that when I manage running .cgi or .sh script, I can easily rename it into .php and put outside cgi-bin directory?
reflex
DD-WRT Novice


Joined: 07 Jun 2007
Posts: 20

PostPosted: Thu Oct 23, 2008 15:29    Post subject: Reply with quote
I think the problem is with the web server, something to do with the way it parses..

I'm using v23 sp2, you can't rename to php if you don't have a php parser installed on the router.

if its not running its most likely a permission issue. try chmod 777 test.sh with the code from before.

David
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 15:41    Post subject: Reply with quote
David, it's really strange, but now I just tried with .sh - and it worked! I downloaded a text file with 'hell world' inside it!
could it be because of ';' sign at the end of the first line? when I removed it and run my script.cgi - it also worked fine, at least within cgi-bin directory.
what do you mean by php parser?
I just need a simple sh script named script.php
although, now I am not that sure it will help me - now I need busybox get a small web-page from Internet and give it back to me through the web-server.
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 15:43    Post subject: Reply with quote
btw, can you point me to any .asp scripts on the router's file system? the thing is they should parse urls! probably, they use the same http environment as cgi.
reflex
DD-WRT Novice


Joined: 07 Jun 2007
Posts: 20

PostPosted: Thu Oct 23, 2008 15:45    Post subject: Reply with quote
Sorry buddy, can't really find any. Plus everything goes to apply.cgi which is internal to the router.. this was really the end of the line for me. You know everything I do right now.
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 15:53    Post subject: Reply with quote
It seems I get it. Look what I did. I put a line
echo "+ ARGS: ${0}"
into my script. When I run it I got the name of my script. Then when I tried adding test.cgi?param=1 I got an error. May be the variables are sent using post method and the server checks that all the requests end with .cgi, .html, .htm - then, it's no good for me, I'd have to go for kamikaze...
redsky
DD-WRT User


Joined: 31 Mar 2008
Posts: 59

PostPosted: Thu Oct 23, 2008 16:18    Post subject: Reply with quote
Aha! That was right assumption. Look what I did: i created a small html page:

Code:

<html>
<form action=http://192.168.0.1/user/cgi-bin/search.cgi method=post>
<textarea name=one></textarea>
<input type=text name=text>
</form>
</html>

Then I created a search.cgi script:

Code:

#!/bin/sh
echo  [ "$REQUEST_METHOD" = "POST" ] && read QUERY_STRING
echo $QUERY_STRING

It worked!
http://www.unixreview.com/documents/s=10116/ur0701i/
so now it's useless as I can't pass parameters in the url.
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Generic Questions All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum