Forum Moderators: coopster & phranque

Message Too Old, No Replies

Executing Shell scripts from web page

Is it possible with perl?

         

eflouret

11:26 pm on Oct 9, 2003 (gmt 0)

10+ Year Member



I have a couple of shell scripts that I would like to execute from a web page.

Cron job is not an option because the point is to execute these scripts whenever I want.

As far as I understand I cannot do this with PHP because I need root access or some kind of similar privilegies.

Is there any way I can do this using perl or any other way?

I mean, I want to execute these shell scripts via a web page.

Any input on this subject will help.

Thanks,

Enrique

sugarkane

5:10 pm on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, you can use backticks to execute any command you have permissions for (or the web server has permissions for).

$foo=`some shell command here`;

$foo will then contain the output of whatever command you issued.

(Note - the command is enclosed in backticks rather than single quotes)

eflouret

5:57 pm on Oct 10, 2003 (gmt 0)

10+ Year Member



Thanks Sugarcane for your reply.

Anyway, since I'm a web designer that can deal with php but very little or no Perl, I will need a further explanation.

$foo contains the variable enclosed within backticks, but how should I execute that command from the script?

Perhaps using SYSTEM or EXEC functions?

Or is it that just by assigning the command to that variable it executes instantly?

Thanks,

Enrique

sugarkane

7:06 pm on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Or is it that just by assigning the command to that variable it executes instantly?

That's it. You're basically telling Perl to set $foo to the result of the command in the backticks, so Perl spawns a shell, executes the command, sets $foo and closes the shell.

After that you can ignore $foo if you want - the command has been executed anyway.

bcolflesh

7:18 pm on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're going to be in the same situation as w/PHP - your user will need to have the correct permissions to execute the commands - if it doesn't work w/PHP, it won't work w/Perl.

eflouret

8:30 pm on Oct 10, 2003 (gmt 0)

10+ Year Member



Thanks everybody.

With my limited programming knowledge (I'm not a programmer) I managed to create a couple simple one liners that performed some lightning fast data extraction from the server log files.

Since I don't have root access permisions, it seems that I will have to find a way of translating those one liners to perl or php.

Thanks again,

Enrique

sugarkane

12:28 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> root access permissions

You *may* be able to arrange permissions for a specific script using sudo [courtesan.com] - if you can persuade your server admin to set it up ;)

bcc1234

12:54 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you host offers cgi (and not mod_perl) then you can just run those scripts through apache-cgi mechanism.

All apache does is executes the specified file in a separate shell process.
Be it
#!/usr/local/bin/perl
or
#!/bin/sh
or whatever you tell is to execute.

That first line tells the shell which interpreter command to use for a file (for all text files with execute bit set).
So set it to the shell of your choice and that's what will be executed.

jatar_k

5:52 pm on Oct 11, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for php what about this
PHP Program Execution functions [ca.php.net]

timster

4:32 pm on Oct 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> Perhaps using SYSTEM or EXEC functions?

Perl has SYSTEM too -- you may be more comfortable using that.

Here's my very stodgy subroutine for doing shell commands and reporting errors.

sub doSystem {
my $cmd = shift();
if (system($cmd) == 0 ) {
return 1
} else {
print "Error executing shell command: $cmd -- $!";
# Remove print line above once goes live?
return;
}
}