in reply to Re^4: Shell to Perl conversion
in thread Shell to Perl conversion

You still don't tell us what you see and how your script fails. "Not getting output" can mean various things. Do you see an 500 error? Do you get another error page? Does the request return a blank page? Does the request never return or time out?

What steps did you undertake to adress the points I mentioned in my first reply?

Now, if you tell us how it's not working, what the error message is, and what you find in the webserver error log, we can maybe help you better.

Be aware that your webserver likely runs your CGI not as your shell user and hence will not have the same permissions as your shell user. Especially, your ~/.ssh2/ directory may or may not be readable to that user, so your passwordless keys for the remote machine won't work.

You don't seem to be checking whether your call to open succeeds. I recommend that you rewrite it as:

my $commandline = "ssh $USER\@$HOST /bin/ping -s $nodeName 64 10 |"; open PING, $commandline or die "Couldn't launch '$commandline': $!/$?";

As another aside, you want to ditch your hand-rolled CGI parser and use CGI instead, which comes with every Perl and has been thoroughly tested.

Replies are listed 'Best First'.
Re^6: Shell to Perl conversion
by rockmountain (Sexton) on Aug 30, 2008 at 18:07 UTC
    I am trying hard to run the below line with sudo, I am not able to handle the password prompt in my script. You are perfectly correct about your observation about permission problem. I have two option for that, either to use ssh command in a shell file and then call it like below:
    `/bin/sh /tmp/rockpingping.sh $nodeName > /tmp/result.txt`;
    The above solution is working, but it is not a good way of doing things. And the second option is using sudo. I am not succeeding sudo option where it password prompt is popping up.
    cheers Rock
    $flag = system("sudo /bin/sh /tmp/sshping.sh > /tmp/result.txt" ); + ## '>' is the redirect operator and test.sh is the shell script If ($flag == 0 ) { // The success result will be stored at result.txt // need to open this file and then use it } else { // failure case }

      You won't be able to handle any password prompt by ssh in your script. This is why I mentioned passwordless keys. Also, there is little difference between `/bin/sh ...` (and throwing the result away) and system(). If the backticks work for you, you can now move the contents of the shell script into that command line and use that.

      You might want to read up about backticks in perlop - you don't need redirection with backticks because they return the script output:

      my @output = `/bin/sh ...`; print "I got: @output";

      Let me repeat: You won't be able to supply a password to sudo or ssh.

        I found out that In my CGI script, the Form data is not being transffered from webpage to underlying script. What could be the workaround for ward of the permission problem between, Operating system user and Webinterface user.
        cheers Rock