The UNIX and Linux Forums

The UNIX and Linux Forums (
-   Shell Programming and Scripting (
-   -   Execution problem unix commands in Perl CGI (

Navrattan Bansa 01-06-2012 11:32 AM

Execution problem unix commands in Perl CGI
I am trying to run SSH , mkdir and other unix commands using Perl CGI. But i am not able to Execute these commands.
Please help me out !!!!
SSH and mkdir is necessity for me.
I will be thankful to you...!!!!!
I am trying like:
In perl CGI file i am writing like: @list = `ssh username@machinename 'ls /global/directory_name'`;

Corona688 01-06-2012 11:52 AM

In what way does it "not work"? What is the code which is "not working"?

From another thread:


Originally Posted by Corona688
It's probably eating the backslash since perl double-quotes evaluate those. You may need to escape the escaping. You'll probably also need to escape those $ in your awk, since perl evaluates those into variables, and escape the double-quotes i the awk statement, so perl won't mistake them for its own.

I remember writing Perl like that: Line after line after line of nothing but system() and backticks [because I wanted to avoid using the shell]. Eventually I noticed they were starting entire bourne shells just to parse one "mv a b" and was horrified -- by avoiding the shell I'd actually used eight of them consecutively :wall:

Returning much later, I rewrote some of these "perl" scripts as shell, making them much smaller, simpler, and saner.


$ wc -l
$ wc -l

---------- Post updated at 10:41 AM ---------- Previous update was at 10:39 AM ----------

Is ssh configured to login without a password, there? You'll probably need to tell it what key files to use with ssh -i /path/to/key. You'll need to give the key the right permissions so the webserver can read it, too.

---------- Post updated at 10:43 AM ---------- Previous update was at 10:41 AM ----------

You may also find it useful to redirect standard error into standard output so you can see error messages from shell programs instead of them all being swallowed by the web server. Unfortunately, while trivial in a shell exec 2>&1 , that's extremely difficult in perl.

---------- Post updated at 10:52 AM ---------- Previous update was at 10:43 AM ----------

"right permissions" so a webserver can read it would probably be 0600 or 0400, owned by apache:root

Navrattan Bansa 01-06-2012 12:23 PM

i am running command like: system("mkdir dirname") and @array= `ssh username@machinename 'ls directoryname'` in which is fine.

If i run the same commands in demo.cgi in web browser then these commands are not working..
PLease help me out...

Corona688 01-06-2012 12:42 PM

Your CGI script gets run as the user apache, probably, which isn't you.

This means:
  • It won't be running in /home/myusername
  • It won't have a decent PATH, so won't find lots of common commands
  • It won't have permissions to mkdir /home/myusername/newfolder
  • It won't try /home/myusername/.ssh/id_dsa for passwordless logins
  • It won't have permission to read /home/myusername/.ssh/id_dsa anyway
  • You won't see your error messages since the CGI interface diverts them all into /dev/null
  • You may not be able to get them back because perl is extremely bad at redirection

I repeat. system() isn't an alternative to shell scripting. system() is an entire, real shell. System is actually the same shell you were hoping to avoid by using perl!

You are running shell scripts.

You are loading entire bourne shells, running single 'mv file1 file2' commands in them, quitting entire shells, then doing it again, over, and over, and over. You are running, potentially, dozens or hundreds of tiny, individual, shell scripts.

And you're running them in an especially slow, wasteful, and bug-prone way, having to tiptoe around perl to get the raw text you wanted into and out of the shell. You can't even get your error messages!

If you post your perl code, I'll show you how to do it in a shell script, and show you how to do it in a way that will work in CGI.

Navrattan Bansa 01-06-2012 12:59 PM

Buddy i am just doing like:
In #This is working well on the Unix terminal which is fine.
system("mkdir -p /dirname/dirname"); # it is creating directory which is fine.
@array = `username@machine_name 'ls dirname'`; # It is giving list of files available on this directory

In Demo.cgi #this is not working well.
use CGI qw(:standard)
system("mkdir -p /dirname/dirname"); # it is not creating directory which is fine.
@array = `username@machine_name 'ls dirname'`; # It is no tgiving list of files available on this directory
@arr = `ls dirname`; # it is working fine
In Demo.html # in this html i am calling Demo.cgi which is not executing ssh & mkdir
<html><head><title>Demo Form</title></head>
<body><form action="Demo.cgi" method="GET">
Enter some text here:
<input type="text" name="sample_text" size=30><input type="submit"><p></form>

Please Give me any solution what should i do... i am almost pissed off with it.. Plezzz.

---------- Post updated at 11:29 PM ---------- Previous update was at 11:28 PM ----------

I have shown you some piece of code which i need no fix ... please give me some solution.

Corona688 01-06-2012 01:24 PM

We are not here 24/7. If you don't get an answer immediately, wait!

Thanks for posting your code. Finally, finally, I can answer some of your questions.


system("mkdir -p /dirname/dirname"); # it is creating directory which is fine.
# ??? Did you forget an 'ssh' in front?
@array = `username@machine_name 'ls dirname'`; # It is giving list of files available on this directory


use  CGI qw(:standard)

# Since you're using system(), which runs inside /bin/sh, you need to
# set a better PATH so the shell can find commands.

# Before apache can write to this directory, you'll need to do this as root:
# chown :apache /dirname
# chmod g+w /dirname
system("mkdir -p /dirname/dirname"); # it is not creating directory which is fine.
# ??? did you forget an 'ssh' in front?
# To run ssh, and get it to work passwordlessly, apache will need access to an id_dsa or id_rsa file somewhere.
# You should create a copy of it and put it somewhere only apache can read.  The file should be owned
# by apache:root.  Then chmod 0400 filename so only apache can read it.  then, when you use ssh, you have to
# tell ssh where it is, like ssh -i /path/to/apache_id_dsa
@array = `username@machine_name 'ls dirname'`; # It is no tgiving list of files available on this directory
@arr = `ls dirname`; # it is working fine

---------- Post updated at 12:24 PM ---------- Previous update was at 12:19 PM ----------

And since your code is nothing but tightly stretched shrinkwrap over 99% shell script, you could also try this CGI script:




# Show error messages.
exec 2>&1

mkdir -p /dirname/dirname

ssh username@host ls dirname

ls dirname

Exactly the same thing as yours, with the shrinkwrap removed, and error messages preserved.

Navrattan Bansa 01-06-2012 01:27 PM

No I am not forgetting ssh there i am creating directory in local Unix machine.
I didn't get exactly your answer.
# chown :apache /dirname ## i didn't gat apache here what does it means here.
# chmod g+w /dirname
where i have to do these actions :
On terminal or in the .cgi script ?

what is the alternative for SSH. I just want to get list of files from there ???

All times are GMT -4. The time now is 11:26 AM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums