Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Shell Command from Web Page?

Shell Command from Web Page?
Thread Tools
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 25, 2003, 01:06 AM
 
Hi,
I was wondering if there's a way to run a shell command from a web page, and print its output to that web page? It seems like PHP should be able to do this sort of thing? For instance, how would I get a web page to display the entire output from "uptime" ?

Thanks!
gabe
     
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
May 25, 2003, 10:09 AM
 
Originally posted by Zimwy:
Hi,
I was wondering if there's a way to run a shell command from a web page, and print its output to that web page? It seems like PHP should be able to do this sort of thing? For instance, how would I get a web page to display the entire output from "uptime" ?

Thanks!
gabe
not having done this in a while, i believe you would do this:
[PHP]
echo `uptime`;
[/PHP]

...anyways, check out the reference pages at www.php.net for backtick usage.
     
Junior Member
Join Date: Mar 2003
Status: Offline
Reply With Quote
May 25, 2003, 10:30 AM
 
just create a simple cgi-script.

-- cut here
#!/bin/sh
echo "Content-type: text/plain\n\n"
uptime
-- cut here

just put it on your cgi-bin directory, add the right permissions, and thats it..
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 25, 2003, 12:53 PM
 
Hey, the backtick thing works great, almost... Okay, I'm a bit stumped by this. If I do:
[PHP]
echo `lsof`;
[/PHP]

It works fine, but if I do:
[PHP]
echo `lsof | awk '/mp3/'`;
[/PHP]
THen I get nothing. The thing is that the second command works perfectly fine in the terminal. I've tried giving full path names to lsof and awk, no luck there. lsof alone works fine. I'm stumped. Any ideas? Other commands, such as uptime work fine as well. Help!

Gabe
     
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
May 25, 2003, 04:51 PM
 
Originally posted by Zimwy:
Hey, the backtick thing works great, almost... Okay, I'm a bit stumped by this. If I do:
[PHP]
echo `lsof`;
[/PHP]

It works fine, but if I do:
[PHP]
echo `lsof | awk '/mp3/'`;
[/PHP]
THen I get nothing. The thing is that the second command works perfectly fine in the terminal. I've tried giving full path names to lsof and awk, no luck there. lsof alone works fine. I'm stumped. Any ideas? Other commands, such as uptime work fine as well. Help!

Gabe
...hmm, beats me why you can't pipe stuff, but why don't you just use php's regular expressions to handle it. assign the backtick output to a variable and then run select what you need...
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 25, 2003, 11:08 PM
 
I'll definitely look into select. I've searched the PHP page for it, and I can't find it. Do you have any hints of where to get started for doing awk and grep-like things within php? Thanks. With the pipe, thing is that it does work. I do it to get uptime into a more readable format by piping it to awk. It just won't work with lsof. I can't pipe lsof to grep, or awk, or anything. Very confusing...

gabe
     
Dedicated MacNNer
Join Date: Feb 2001
Location: Manhattan
Status: Offline
Reply With Quote
May 26, 2003, 06:42 AM
 
Originally posted by Zimwy:
I'll definitely look into select. I've searched the PHP page for it, and I can't find it. Do you have any hints of where to get started for doing awk and grep-like things within php? Thanks. With the pipe, thing is that it does work. I do it to get uptime into a more readable format by piping it to awk. It just won't work with lsof. I can't pipe lsof to grep, or awk, or anything. Very confusing...

gabe
...i didn't mean run the "select" function. i meant run a regular expression to "select" what you want. sorry for the misunderstanding...
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 26, 2003, 06:02 PM
 
Have you tried it with the exec command? A lot of contributors have noted that the backtick method sends your commands through a shell, whilst exec calls your commands directly.
Computer thez nohhh...
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 26, 2003, 06:41 PM
 
Hey,
No dice there either.

Thanks though
     
Junior Member
Join Date: Mar 2003
Status: Offline
Reply With Quote
May 27, 2003, 12:05 AM
 
Originally posted by Simon Mundy:
Have you tried it with the exec command? A lot of contributors have noted that the backtick method sends your commands through a shell, whilst exec calls your commands directly.
err..

the only difference its that exec does not spawn a new shell to run the command.

so in this case there its no difference..
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2