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

I have taken care of ssh authenitcation. When I run my shell script from command line the output is:
[User@Hostname ~]$ ./ping.sh PING target 56(84) bytes of data. 64 bytes from target: icmp_seq=0 ttl=64 time=0.339 ms 64 bytes from target: icmp_seq=1 ttl=64 time=0.388 ms 64 bytes from target: icmp_seq=2 ttl=64 time=0.342 ms Killed by signal 2. [User@Hostname ~]$
I am trying to achieve the same thing in webinterface. I am not getting the output in the webpage. The compiler has no complain. Here goes the complete code.
cheers Rock
#!/usr/bin/perl #domain name suffix - if we need it $domainName=""; # Get the input $buffer = $ENV{'QUERY_STRING'}; @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } print "Content-type: text/html\n\n"; print <<__HTML__; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <TITLE>Rockmountain&reg;/Webinterface&trade;</TITLE> <STYLE type="text/css"> h1.headerTool { font-size: 30pt; font-style: italic; font-weight: bold; letter-spacing: -4px; } .headerReg, .headerTM { font-size: 12pt; vertical-align: super; } .headerWebinterface { font-size: 28; font-weight: 500; letter-spacing: 0px; } body { background-color: #000033; color: #ffffff; font-family: Arial, Verdana, sans-serif; } .systemMsg { font-size: 14pt; font-weight: bold; } </STYLE> </HEAD> <BODY> <h1 class="headerTool">NETCOOL<span class="headerReg">&reg;</span>/ <s +pan class="headerWebinterface">Webinterface</span><span class="header +TM">&trade;</span></h1> <CENTER> <HR HEIGHT="15" > </CENTER> __HTML__ $nodeName = "seres"; if (! $FORM{"\$selected_rows.Node"}) { print "<p class=\"systemMsg\">No Node Specified.</p></body></h +tml>"; die; } else { $nodeName = $FORM{"\$selected_rows.Node"}.$domainName; print "<p class=\"systemMsg\">Pinging host ".$nodeName."</p>\n +"; } $USER = "User"; $HOST="hostname"; $CMD="/usr/sbin/ping"; #open(PING,"/usr/sbin/ping -s ".$nodeName." 64 10 |"); open(PING,"ssh ".$USER."@".$HOST." /bin/ping -s ".$nodeName." 64 10 +|"); $old_fh = select(STDOUT); $| = 1; select($old_fh); print "<PRE>"; while(<PING>) { print; } print "</PRE>"; print "<CENTER><HR HEIGHT=\"15\" ></CENTER>"; print <<__HTML__; <form action=""> <div align="center"><input type="button" value="Close Window" onClick= +"javascript:window.close();"></div> </form> </BODY> </HTML> __HTML__

Replies are listed 'Best First'.
Re^5: Shell to Perl conversion
by Corion (Patriarch) on Aug 29, 2008 at 10:37 UTC

    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.

      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.