Goffredo Saffioti wrote:
> 
> Hi all list.

Hello,

> I'm attempt to write a perl script for to migrate from bind dns to
> djbdns all my new incoming domains, the scenario is as follow:
> In the production environment I have on 2 servers a folder that contain
> request from people for domains activation, the informations for the
> request domain are mantained in a text file on this form:
> 
> [root@magellan mydocuments]# cat D0001102.txt
> 
> saffiot.it;  <--- the domain name
> webtu02;     <--- the webserver for the website
> mail01-ichi; <--- the mail server disigned
> 
> and for that's part all was fine because i'm write a script that read
> from this file the informations and write in another what I need and
> also a string that identify the domain, the alias for the web server &
> the mail address.

The main problem is that when YOU run the script it works because YOU
own the file (/home/gsaffioti/...) but when the WEB SERVER runs the
script it runs as a different user that doesn't have access to YOUR
files.  Read perlfaq9 for more information on programming in a CGI
environment.

perldoc perlfaq9


> the script is as follow:
> 
> [root@magellan mydocuments]# cat translation.pl
> 
> #!/usr/bin/perl -w
> open (MYFILE,
> '</home/gsaffioti/mydocuments/not_processed_users/D0001102.txt') ||
> &error ;
> 
> sub error {
> print "cannot stat\n";

Your error message does not contain any useful information.  You should
include the $! variable so you know WHY open failed.  Also, if the open
failed you still try to read from the file even though the file handle
is not valid.

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

my $file =
'/home/gsaffioti/mydocuments/not_processed_users/D0001102.txt';

open MYFILE, $file or die "Cannot open $file: $!";


> }
> 
> @array=<MYFILE>;

You are only using the first line of the file so there is no reason to
read the entire file into an array and you only need the data in the
first line up to the semicolon.

( my $host = <MYFILE> ) =~ s/;.*//s;
die "Cannot find host name in $file\n" if !defined( $host ) or $host eq
'';


> if (@array > 0) {
> 
> open (SOA, ">/home/gsaffioti/mydocuments/data");
> $soa="$array[0]";
> chop ($soa);
> $soa=~ s/;/:80.18.33.190:86400:a/;
> print SOA ".$soa\n" ;
> close (SOA);
> 
> open (SOA2, ">>/home/gsaffioti/mydocuments/data");
> $soasl="$array[0]";
> chop ($soasl);
> $soasl=~s/;/:80.18.33.189:86400:b/;
> print SOA2 ".$soasl\n";
> close (SOA2);
> 
> open (MAIL, ">>/home/gsaffioti/mydocuments/data");
> $mail="$array[0]";
> chop ($mail);
> $mail=~ s/;/:80.18.33.190:a/;
> print MAIL "\@$mail\n";
> close(MAIL);
> 
> open (EQU, ">>/home/gsaffioti/mydocuments/data");
> $equ="$array[0]";
> chop $equ;
> $equ=~ s/;/:80.18.33.181:86400/;
> print EQU "+www.$equ\n";
> close(EQU);
> close (MYFILE);
> }

Again, you should ALWAYS verify the file opened correctly before trying
to write to it.

my $data = '/home/gsaffioti/mydocuments/data';

open OUT, '>', $data or die "Cannot open $data: $!";
print OUT ".$host:80.18.33.189:86400:b\n";
print OUT ".$host:80.18.33.189:86400:b\n";
print OUT "\@$host:80.18.33.190:a\n";
print OUT "+www.$host:80.18.33.181:86400\n";
close OUT


> -------------------------------------------------------------
> 
> And this is the data file :
> 
> [root@magellan mydocuments]# cat data
> 
> .saffiot.it:80.18.33.190:86400:a
> .saffiot.it:80.18.33.189:86400:b
> @saffiot.it:80.18.33.190:a
> +www.saffiot.it:80.18.33.181:86400
> 
> So you can see that the script work for only 1 file.
> 
> But if I trye to add the part that must read all files in a directory
> that contain all other files this script drive me grazy!
> because i receive an error that don't know why exist!.
> 
> the part that I attempt to add is as follow:
> 
> #!/usr/bin/perl -w
> opendir (DIR, '/home/gsaffioti/mydocuments/not_processed_users') ||
> &error;
> @dir= readdir (DIR);
> close (DIR);
> if (@dir) {
> print "Start Distillation Process for domains wait plz:\n";
>     foreach $file (@dir) {
> print "$file\n" unless
>     ($file=~/^\.+$/);
> 
> open (MYFILE, '</home/gsaffioti/mydocuments/not_processed_users/$file')
> || &error;
> @array=<MYFILE>;
> 
> &processing;     ###this recall the previous code for the file reading.
> }
> 
> sub error{
> print "stopped here\n";

The error message says you stopped but you don't actually stop the
program.


> }
> 
> sub processing{
> 
> here all the previous code whithout the handle MYFILE for the specific
> file that now become MYFILE for $file and i'm here not sure!!!.
> So now if I run the script this error occur:
> 
> [root@magellan mydocuments]# ./filehandle.pl
> 
> Start Distillation Process for domains wait plz:
> stopped here
> readline() on closed filehandle MYFILE at ./filehandle.pl line 15.
> stopped here
> readline() on closed filehandle MYFILE at ./filehandle.pl line 15.
> D0001102.txt
> stopped here
> readline() on closed filehandle MYFILE at ./filehandle.pl line 15.
> 
> Seem that I attempt to write on a close filehandle but nothing is
> changed in the other code that can close the filehandle is the $file
> variable that is wrong? or what.
> Anybody know whereis the trouble??

You need to include the $! variable in your error messages to get any
useful information on WHY open or opendir, etc. failed.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to