Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Simple Perl print() output redirect to html/webpage.

by hmb104 (Sexton)
on Sep 03, 2012 at 05:36 UTC ( #991356=perlquestion: print w/replies, xml ) Need Help??
hmb104 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl monks,

I have a Perl script that I use to extract some info from my webserver. The script works great on the command line. It takes a username as an argument and it returns few lines of output such as below:

./ hmb104 #======================================================== # info for [hmb104] # Time: Sun Sep 2 18:18:38 CDT 2012 #======================================================== ID: hmb104 home: /home/usr/hmb104 ---------------------------------------------------------- usage: 45.2 Dynamic: 33 ---------------------------------------------------------- End of report

The script returns print() statements with some variables. I would like to know how to make the script print the output to a webpage. I have tried to exec() the script in PHP but for userid problems it didn't work. How I can make the script redirect output to html document?

The script is located in "server1" and the webserver is "server2". I created a PGP key so I can execute the script from server2 without any problem.

I appreciate the input

Replies are listed 'Best First'.
Re: Simple Perl print() output redirect to html/webpage.
by bitingduck (Chaplain) on Sep 03, 2012 at 06:29 UTC

    Last time I did something like that, I just used a shell script wrapped around my perl scraper, and did the upload with scp:

    #!/bin/bash # scrape targets and load files to server . $HOME/.bash_profile cd $HOME/MyBot FILENAME=`date "+scrapefiles/scrapetarget%C%y%m%d.txt"` perl > $FILENAME scp $FILENAME

    "templistings" is the remote directory I load my scrapings into, where a cron job looks for them periodically and loads them into a mysql database. In your case you would just specify the directory where you want your file to go in your web structure and just write it as a .html file. You could delete the local copy after, but I keep mine in an archive.

    An alternative would be to use something like Net::SCP to do the whole thing from within your perl program. You could probably also have your program ssh in to server2 (with Net::SSH::Perl) and write the file directly without making a local copy, but that seems a little more fragile to me.

    If the problem is that you need it to run from inside a PHP page, that's a PHP problem, and there's probably better places to ask (though some here might be able to help).

      Thanks for taking the time to reply bitingduck. Actually my question is about displaying my Perl output to html webpage is on server1 and html page runs on server2. From server2 I can call my Perl script from server2 as following:

      ssh myusername@server1 ./ arg

      The script will display some output. I want to know how I can capture this output on test.html on server2?

      server2 is a pure webserver. All my servers are Unix

        This question really isn't in Perl-land any more, but a CGI question. This CGI script should do it:

        #!/bin/sh echo "Content-Type: text/plain" echo ssh myusername@server1 ./ arg

        I'm not sure why your php solution did not work. Maybe you wanted the passthru function?

        Are you directing your output from the script to the right place? And are the permissions set correctly for you to be able to write there? As anon pointed out, it's not obvious why it's not working for you. When you mention user id problems with it, that suggests that it might be working fine except that you don't have write permission to the web server path from whatever user you're running the script as.

Re: Simple Perl print() output redirect to html/webpage.
by aitap (Curate) on Sep 03, 2012 at 07:59 UTC

    If you need to write everything thet print writes to a file, use select:

    open my $html, ">", /srv/www/mystatus.htm; select $html;
    If you want to call your Perl script from a webserver, you need to use CGI or CGI::Simple.

    Please specify what exactly you want.

    Sorry if my advice was wrong.

      My problem with using PHP passthru() is that it uses the user nobody to execute. The user nobody doesn't have write access & also PGP will not work with nobody and it is not recommended to give such access to nobody.

      My script returns 6 lines of print() statements, I dont want to make it a CGI script and add all the CGI related info to it. Can I create another CGI script and call it from within just to print my lines?

        Can I create another CGI script and call it from within just to print my lines?
        Why not? You can use the backtick operator or do + Capture::Tiny to get the stdout of your script.
        Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991356]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-06-23 20:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.