Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Perl script - output to display via PHP in a browser

Hi guys!

I have the following Perl script that works fine.
What Id like to do with it is:

By utilizing my current perl code, use PHP to:
1) Build a form somehow where I could manually enter the name of the computer system list (instead of hard coding into my script..currently set to list.xt)
2) Click a Run button to execute the script
3) Write the output to a browser window, maybe in a 3 column window, eg. 1 for notexist.txt, 1 for olderthan2.txt", the other for recent.txt Here is the perl code:

----------------------------------------------------------------------------------- script.pl
#!/usr/bin/perl -w
use strict;
use warnings;

open(NOTEXIST, ">notexist.txt") or die "Can not create notexist: $!\n";
open(OLD, ">olderthan2.txt") or die "Can not create olderthan2: $!\n";
open(RECENT, ">recent.txt") or die "Can not create recent: $!\n";

open(LIST,"<list.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      system("net use s: \\$server\ipc\$");
      if(!-e 's:\password.txt') {
            print NOTEXIST "$server doesnt exist\n";
      }
      elsif(-M 's:\password.txt' > 2) {
            print OLD "$server " . localtime( (stat('s:\password.txt'))[9] ) . "\n";
      }
      else {
            print RECENT "$server " . localtime( (stat('s:\password.txt'))[9] ) . "\n";
      }
}
close(LIST);
close(RECENT);
close(OLD);
close(NOTEXIST);

---------------------------------------------------------------------------------------------- End of script.

So, in the above, instead of displaying the results in:
open(NOTEXIST, ">notexist.txt") or die "Can not create notexist: $!\n";
open(OLD, ">olderthan2.txt") or die "Can not create olderthan2: $!\n";
open(RECENT, ">recent.txt") or die "Can not create recent: $!\n";

the results could appear in a browser.

Any help greatly appreciated.
SOLUTION
Avatar of Rurne
Rurne
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjcoyne
mjcoyne

Why not do it all in Perl?
Avatar of Simon336697

ASKER

Hi mj!
Can you do this all in Perl mjcoyne! And display it in a browser?
Would you typically use the LWP module for this?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Perl Diver...

Thank u so much for your help.
I ran your code, but not getting any success.

--------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;

use CGI qw/:standard/;
print header, start_html(-title=>'your webpage title');

#open(NOTEXIST, ">notexist.txt") or die "Can not create notexist: $!\n";
#open(OLD, ">olderthan2.txt") or die "Can not create olderthan2: $!\n";
#open(RECENT, ">recent.txt") or die "Can not create recent: $!\n";

open(LIST,"<c:\\dood\\list.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      chomp($server);
#      system("net use s: \\$server\\pass\$ \"password\" /user:$server\\administrator > NUL");
  system("net use s: \\$server\\pass\$");
      if(!-e 's:\password.txt') {
            print "$server doesnt exist<br/>\n";
      }
      elsif(-M 's:\password.txt' > 2) {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
      else {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
}
close(LIST);
#close(RECENT);
#close(OLD);
#close(NOTEXIST);

print end_html;

-------------------------------------------------- Output:

C:\dood>nn.pl
Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>your webpa
e title</title>
</head><body>System error 67 has occurred.

The network name cannot be found.

predator doesnt exist<br/>
</body></html>
C:\dood>
====================================================
predator is the name of my local machine, and the only entry in list.txt

If i do a net use s: \\predator\pass$,
I get a connection no problems.
I changed the code to:

#!/usr/bin/perl
use strict;
use warnings;

use CGI qw/:standard/;
print header, start_html(-title=>'your webpage title');

#open(NOTEXIST, ">notexist.txt") or die "Can not create notexist: $!\n";
#open(OLD, ">olderthan2.txt") or die "Can not create olderthan2: $!\n";
#open(RECENT, ">recent.txt") or die "Can not create recent: $!\n";

open(LIST,"<c:\\dood\\list.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      chomp(my $server);
#      system("net use s: \\my $server\\pass\$ \"password\" /user:$server\\administrator > NUL");
  system("net use s: \\my $server\\pass\$");
      if(!-e 's:\password.txt') {
            print "my $server doesnt exist<br/>\n";
      }
      elsif(-M 's:\password.txt' > 2) {
            print "my $server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
      else {
            print "my $server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
}
close(LIST);
#close(RECENT);
#close(OLD);
#close(NOTEXIST);

print end_html;

====================================== Output:


C:\dood>nn.pl
Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>your webpag
e title</title>
Use of uninitialized value in scalar chomp at C:\dood\nn.pl line 14, <LIST> line
 1.
Use of uninitialized value in concatenation (.) or string at C:\dood\nn.pl line
16, <LIST> line 1.
</head><body>System error 67 has occurred.

The network name cannot be found.

Use of uninitialized value in concatenation (.) or string at C:\dood\nn.pl line
18, <LIST> line 1.
my  doesnt exist<br/>
</body></html>
C:\dood>
With the first example, try this for your system command:
system("net use s: \\\\$server\\pass\$");

Well, this is wrong:

while (my $server=<LIST>) {
      chomp(my $server);

you can't use "my" twice on the scalar $server in the same scope otherwise $server will have no value, hench the warnings about an undefined value in chomp and concatenation. Should be like I had it:

while (my $server = <LIST>) {
      chomp($server);

is it possible there are blank lines in the file you are looping through? Also try Adams suggestion about properly escaping the backslashes in the system function argument.
I see you have "my" in the print lines too. They will not affect the script as they are treated as the literal string "my" and not the function "my" but you only use "my" once to decalre a variable within a block of code.

http://perldoc.perl.org/functions/my.html 
============================================================ nn.pl

#!/usr/bin/perl
use strict;
use warnings;

use CGI qw/:standard/;
print header, start_html(-title=>'your webpage title');

open(LIST,"<list.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      chomp($server);
  system("net use s: \\$server\\pass\$");
      if(!-e 's:\password.txt') {
            print "$server doesnt exist<br/>\n";
      }
      elsif(-M 's:\password.txt' > 2) {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
      else {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
}
close(LIST);

print end_html;
=============================================================== Output

C:\>nn.pl
Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>your webpag
e title</title>
</head><body>System error 67 has occurred.

The network name cannot be found.

predator doesnt exist<br/>
</body></html>
C:\>
=======================================================
If I separately do a net use s:, its fine.
Ok it looked like it was because i had:

\\$server
instead of
\\\\$server

----------------------------Output now is:
C:\>nn.pl
Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>your webpag
e title</title>
</head><body>predator Mon Jul 30 13:02:42 2007<br/>
</body></html>
C:\>
--------------------------------------------------
But a web page is not created.
The script is intended to be run from a webserver.  When a browser calls the script, it will be displayed in the browser.

Is this not what you wanted?

If you redirect the output to a .html file, you can then open that with a browser (you'll probably want to remove the header first though).

eg:
at a prompt, type:    nn.pl > test1.html
edit test1.html using a text editor, remove the "Content-Type" line, and the blank line, save file
Use web browser to view test1.html (at prompt, type "test1.html", but no quotes)
Hi Adam!

Yes youre right, it is what I wanted to be shown in a web browser.

http://localhost/nn.pl

It just comes up with a File Save/Open dialog.
When I click on Open, it just displays the code in the browser.
Im my Perl libs:

C:\Perl\lib\CGI

 Volume in drive C has no label.
 Volume Serial Number is 74DC-C9FB

 Directory of c:\perl\lib\cgi

16/07/2007  09:08 AM    <DIR>          .
16/07/2007  09:08 AM    <DIR>          ..
19/06/2002  09:26 PM               297 Apache.pm
14/07/2002  07:23 AM            13,112 Carp.pm
19/06/2002  09:26 PM            13,658 Cookie.pm
16/07/2007  09:08 AM    <DIR>          eg
19/06/2002  09:26 PM             7,423 Fast.pm
19/06/2002  09:26 PM             7,262 Pretty.pm
21/03/2001  11:31 AM            11,267 Push.pm
19/06/2002  09:26 PM               312 Switch.pm
19/06/2002  09:26 PM            11,039 Util.pm
               8 File(s)         64,370 bytes
               3 Dir(s)  235,949,752,320 bytes free
If I do:

nn.pl > test1.html

and open test1.html in a browser:

-------------------------------------------------- output
Content-Type: text/html; charset=ISO-8859-1 predator Mon Jul 30 13:02:42 2007
-------------------------------------------------

Could it be because of the header in my code, or lack of header?

--------------------------------------------------------- nn.pl
#!/usr/bin/perl
use strict;
use warnings;

use CGI qw/:standard/;
print header, start_html(-title=>'your webpage title');

open(LIST,"<list.txt") or die "Can not open list: $!\n";
while (my $server=<LIST>) {
      chomp($server);
     
  system("net use s: \\\\$server\\pass\$ > NUL");
      if(!-e 's:\password.txt') {
            print "$server doesnt exist<br/>\n";
      }
      elsif(-M 's:\password.txt' > 2) {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
      else {
            print "$server " . localtime( (stat('s:\password.txt'))[9] ) . "<br/>\n";
      }
}
close(LIST);

print end_html;
----------------------------------


http://localhost/nn.pl

It looks like this is being executed, but is at a command line, just not displaying in a browser.
Sorry, NOT being executed it should be.
So, http://localhost/nn.pl is NOT working.
It sounds as though your webserver is not configured to execute the script.

Which webserver are you running?
Does it work if you put nn.pl in the cgi-bin subdirectory?
Hi Adam!

c:\wamp\www
The above is the document root.

C:\wamp\Apache2\cgi-bin
This is my cgi-bin folder

Ive been trying to do the following:
http://localhost/nn.pl
and have placed password.txt, nn.pl, and list.txt in c:\wamp\www

If I need to place all these in c:\wamp\Apache2\cgi-bin
then how would I point to nn.pl from my web browser?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Adam...that worked mate thank you...and to everyone.

Just on the same thing quickly...

In the same directory as nn.pl (eg.c:\wamp\apache2\cgi-bin), there is a demo perl script to print env variables..

The code is:----------------printenv.pl

#!c:/perl/bin/perl.exe
##
##  printenv -- demo CGI program which just prints its environment
##
print "Content-type: text/html\n\n";

foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    print "${var}=\"${val}\"\n";
}

===================================
http://localhost/cgi-bin/printenv.pl gives me...................................................

COMSPEC="C:\WINDOWS\system32\cmd.exe" DOCUMENT_ROOT="C:/wamp/www" GATEWAY_INTERFACE="CGI/1.1" HTTP_ACCEPT="*/*" HTTP_ACCEPT_ENCODING="gzip, deflate" HTTP_ACCEPT_LANGUAGE="en-au" HTTP_CONNECTION="Keep-Alive" HTTP_HOST="localhost" HTTP_UA_CPU="x86" HTTP_USER_AGENT="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)" PATH="C:\Perl\bin\;C:\Program Files\Windows Resource Kits\Tools\;c:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\IDM Computer Solutions\UltraEdit-32;C:\Handy;C:\WAMP\;C:\WAMP\MySQL\Bin;" PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH" QUERY_STRING="" REMOTE_ADDR="127.0.0.1" REMOTE_PORT="2458" REQUEST_METHOD="GET" REQUEST_URI="/cgi-bin/printenv.pl" SCRIPT_FILENAME="C:/wamp/Apache2/cgi-bin/printenv.pl" SCRIPT_NAME="/cgi-bin/printenv.pl" SERVER_ADDR="127.0.0.1" SERVER_ADMIN="webmaster@localhost" SERVER_NAME="localhost" SERVER_PORT="80" SERVER_PROTOCOL="HTTP/1.1" SERVER_SIGNATURE="" SERVER_SOFTWARE="Apache/2.2.4 (Win32) PHP/5.2.3" SYSTEMROOT="C:\WINDOWS" WINDIR="C:\WINDOWS"
------------------------------------------------------------------------------------
Thanks guys.
If you add a <br /> to the print statement, it'll make it easier to read from a webpage:
    print "${var}=\"${val}\"<br />\n";
Thanks Adam!