Execution problem unix commands in Perl CGI


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution problem unix commands in Perl CGI
# 1  
Old 01-06-2012
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'`;

Last edited by Navrattan Bansa; 01-06-2012 at 12:35 PM.. Reason: Sample command which i m trying
# 2  
Old 01-06-2012
In what way does it "not work"? What is the code which is "not working"?

From another thread:

Quote:
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 Smilie

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

Code:
$ wc -l dispatch.pl
49 dispatch.pl
$ wc -l dispatch.sh
25 dispatch.sh
$

---------- 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
# 3  
Old 01-06-2012
i am running command like: system("mkdir dirname") and @array= `ssh username@machinename 'ls directoryname'` in demo.pl. 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...
# 4  
Old 01-06-2012
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.

Last edited by Corona688; 01-06-2012 at 01:49 PM..
# 5  
Old 01-06-2012
Buddy i am just doing like:
In Demo.pl #This is working well on the Unix terminal which is fine.
#!/usr/bin/perl
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.
#!/usr/bin/perl
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>
</body></html>


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.
# 6  
Old 01-06-2012
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.

Code:
#!/usr/bin/perl
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

Code:
#!/usr/bin/perl
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.
$ENV{PATH}="/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin";

# 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:

Code:
#!/bin/sh

PATH="/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin";

# 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.
# 7  
Old 01-06-2012
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 ???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl execution commands

I don't know to debug the program todaylive.pl program. plz someone let me know what are the commands I need to know to debug the perl programs to find out the error on it. (3 Replies)
Discussion started by: ramkumar15
3 Replies

2. Shell Programming and Scripting

Perl CGI : unable to download the excel sheet from perl cgi page

Hi All, I have written an cgi perl script that displays an image(Excel image) and when clicked on that Image I need to download a excel sheet. I made sure that excel sheet exists in the folder with the given name but still I am not able to download the sheet. print "<center><table... (2 Replies)
Discussion started by: scriptscript
2 Replies

3. Shell Programming and Scripting

Perl cgi pages out of cgi-bin folder in WINDOWS

Hi team, I have a typical problem with cgi pages in apache webserver in WINDOWS I am able to execute(display) the pages that are saved in cgi-bin folder. But I am not able to execute the pages stored in htdocs or other folder other than cgi-bin folder. Could anyone please let me know how... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

5. Web Development

cgi script and external UNIX commands (like swadm)

Hi, I am trying to implement a server monitoring dashboard using cgi scripting. I am planning to run the necessary unix scripts from the web page using cgi. This method works fine for standard unix commands but I am unable to run some external unix commands (like swadm show_processes, swadm... (9 Replies)
Discussion started by: jofinjoseph
9 Replies

6. Shell Programming and Scripting

Execution problem with perl

I got the below error when using the below code...it seem that perl interpret the "'" in the middle and therefore the pipe is not finished. perl -wle ' @a=`who| perl -wlane 'print \$F;' | sort -u` ; chomp @a ; print @a; ' the error message in cygwin is:- perl: No match. | sort... (12 Replies)
Discussion started by: ahmad.diab
12 Replies

7. UNIX Desktop Questions & Answers

CGI execution problem on Unix

Hello everyone, I have developed a cgi application on windows. i have created a folder in webapps of tomcat, within that a WEB-INF folder and in that cgi folder. This cgi folder contains one executable. The web.xml is as follows: <servlet> <servlet-name>cgi</servlet-name> ... (2 Replies)
Discussion started by: Preeti_t
2 Replies

8. Solaris

Solaris 10.5 perl and cron job execution problem

Hi, I want to run a crontab job on solaris 10.5. I have configured the crontab accordingly 10 * * * * /scripts/dbalter.pl >> /scripts/cronout.txt However this does not work .Then I go to /var/mail/root and find an error in the output: From root@myserver Wed Feb 4 17:02:00 2009... (1 Reply)
Discussion started by: sonu2die4
1 Replies

9. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (6 Replies)
Discussion started by: ran16
6 Replies

10. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (0 Replies)
Discussion started by: ran16
0 Replies
Login or Register to Ask a Question