Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unix Perl and Html

by meccaxlr (Initiate)
on Aug 15, 2001 at 22:56 UTC ( #105139=perlquestion: print w/ replies, xml ) Need Help??
meccaxlr has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys I have a summer project to do. My employer wants this snazzy webpage of the status of the printers in my computer lab. I can run a command called "sudo printstatus" in unix under admin and it shows me the status of the printers in a text file. Now does anyone know how to make a simple program (or even have a simple program) that can take the input from the a command into a log file, and make that file readable by html (because i want to put the status on a web page) ?? any suggestions... any? Thanks for your time.

Comment on Unix Perl and Html
Re: Unix Perl and Html
by gryphon (Abbot) on Aug 15, 2001 at 23:24 UTC

    Greetings,

    This might give you an outline to get started. It's a fairly nasty and rapidly punched in piece of code, so improvements are certainly required.

    #!/usr/bin/perl -wT use strict; use CGI 'header'; my $data = `sudo printstatus`; open(LOG, '>> some.log.file') || die "Badness: $!\n"; print LOG $data, "\n\n", '=' x 75, "\n\n"; close(LOG); print header; print "<HTML><HEAD><TITLE>sudo printstatus</TITLE></HEAD>\n"; print "<H2>sudo printstatus</H2>\n<PRE>"; open(READLOG, 'some.log.file') || die "Badness: $!\n"; while (<READLOG>) { s/</&lt;/g; s/>/&gt;/g; print; } close(READLOG); print "</PRE></BODY></HTML>\n";

    Of course, you can also parse the output of your command and put it into a nice looking table or something. Also, this only logs stuff when the user actually accesses the CGI. Also, it appends the new data to the end of the log rather than the beginning. That might be annoying for longer files.

    -gryphon
    code('Perl') || die;

      And a many thanks to you too gryphon monk ! My boss told me that the sudo thing was a bad idea because of security loopholes,(a mistake on my part) reading in from three dumpfiles in /usr/lsys/printstatus would be a better idea because its the output of sudo printstatus anyways. what would be a good way to do this. and i've looked at the dump files and they are a night mare, how would i parse them, i'm still a beginner any online resources for parsing? Thanks alot. your imput is greatly appreciated.

        Greetings meccaxlr,

        I'd be happy to help you, but I'm going to need to see some examples of the three files you're talking about. Also, how do you want them to be displayed? Who's your audience? That'll make a big difference. Also, and more importantly, what are folks looking at this report going to use it for? What are they wanting to learn from it?

        If you answer those questions and post short examples of the three files, I'm sure we can come up with something extra nifty-spiffy.

        -gryphon
        code('Perl') || die;

Re: Unix Perl and Html
by thatguy (Parson) on Aug 15, 2001 at 23:29 UTC
    First off, I would not run the printstatus command from your script. This is a big security risk and most likely, the user your webserver uses is not allows sudo access (since it is implicitly defined by the admin).

    Now a cronjob running printstatus into a logfile would be better. check out the man page for cron.. something like 0 0 * * * sudo /path/to/printstatus >> /path/to/logfile should work ok.

    Depending on the output of printstatus (got a sample?) you could read it in line by line and print out the html using the CGI.pm.. also something like

    #!/usr/bin/perl -w use strict; use CGI; my $query=new CGI(); print $query->start_html(-title=>'Printer Status'); open(LOG," /path/to/logfile") || die "Cannot open logfile!: $!\n"; while(<LOG>) { print $query->br,"$_ ",br; } close(LOG); exit;

    -p
      Thanks for your help thatguy monk! for security reasons my boss told me not to run a cron job for sudo printstatus. He told me to have my program read form 3 dump files in usr/lsys/printstatus which is the output of printstatus. He said running a sudo would be a bad idea. from there all i have to do is parse the files, any suggestions? Thanks a billion !
        do you have some examples of the output of printstatus? (you can change the names/ips to protect your network, we just want the formatting).

        If the output is a single line per printer you can use my previous example, otherwise there might be some work involved :)

        Update:if the output is something along the lines of

        name display toner level ----------------------------------- printer1 toner low 25% printer2 toner low 1% printer3 etc etc etc

        then this should work ok..

        #!/usr/bin/perl -w use strict; use CGI; my $main=new CGI; print $main->header; print $main->start_html("Printer Status"); open(STATUSFILE," your_outfile") || die "Cannot open logfile!: $!\n"; while(<STATUSFILE>) { chomp; if (! ($_=~ /-----------------------------------/)) { #my ($printer,$display,$toner,$level) = split(/ /, $_) +; my @status = split(/ /, $_); if (($status[0] eq "name") && ($status[1] eq "display" +)) { print "<center><table width=250 border=1><tr a +lign=center>\n"; for (@status) { print "<td><b>$_</b></td>\n"; } print "</tr>\n"; } else { for (@status) { print "<td>$_</td>\n"; } print "</tr>\n"; } } } print "</table>\n"; close(STATUSFILE); exit;

        -p

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://105139]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-18 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls