1
votes

I'm trying to run ssh, mkdir from a Perl CGI script. It's not working. But in normal Perl script, it is working fine. Can any one tell me how to run commands in a Perl CGI script?

3
The actual code would be interesting. Which command works in a "normal script" but not in "CGI"? What error message do you get ( $! )?Egga Hartung
In demo.pl i am using system("ssh username@machinename 'ls /directoryname'") IN demo.cgi i am using same command its not working, even system command for creating directory working well in normal perl script. but not in perl cgi. the command is: system("mkdir dirname");Navrattan Bansal
system does not capture stdout. If you see it "working" when trying the script in bash, it's because you see the stdout in the background of the perl process, printed to the terminal. This data is not available to perl, and probably won't be to the webserver either. Use qx()or backticks to capture stdout.TLP

3 Answers

7
votes

If you're running this script via a webserver, chances are the active user (e.g. "nobody", "www", etc) may not have the necessary privileges to execute commands like mkdir and ssh. If so, that is not something that perl can fix. You can test who the active user is with something like:

print "The active user is: ", `whoami`;

It is also a security concern, to have your web user privileges set to create files and perform commands.

0
votes

system() or popen() are probably what you're looking for, if you're feeling dirty I think you can use back ticks too.

0
votes

Do you need to run unix commands? Perl has a built-in mkdir, and there are modules to handle SSH. Normally a CGI process is going to have limited capabilities or access to the system. The more you can do in Perl the better.