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

Hello we just got servers setup through load balancers, now the question comes up, since we update files regularly, not just database files, how can we use Perl to open a file on a server that is connected through a local lan line and append or write to the file?

We would appreciate any advice on this.

thank you.
Richard

Replies are listed 'Best First'.
Re: Updating files over multiple servers
by andreas1234567 (Vicar) on Sep 27, 2007 at 09:20 UTC
    Perhaps File::Rsync using rsync could help you to keep the file systems synchronized.
    --
    Andreas

      I'd have to agree with andreas1234567. rsync or rdist are the way to go. You don't say what type of server you're load balancing but I'm going to assume web servers. If that's the case, you should chose one machine (and probably not one being load balanced) as a *staging* server. You would then promote from your qa server (you do qa, right?) to the staging server via whatever repository mechanism you have. Once the files are on your staging server, then you rsync or rdist out to the your load balanced machines.

      -derby
Re: Updating files over multiple servers
by svenXY (Deacon) on Sep 27, 2007 at 08:14 UTC
Re: Updating files over multiple servers
by sago (Scribe) on Sep 27, 2007 at 06:55 UTC

    # A sample program

    # Modules used
    use Net::FTP;
    use File::Copy;
    Net::FTP;

    my $error,$ftp;

    #Connection to the test server
    $ftp = Net::FTP->new("testserver.vago.com", Timeout => 75, Debug => 0, BlockSize => 1024);

    if (!$ftp) {
    print "Could not connect to testserver.vago.com\n";
    }
    else
    {
    print "Successfull Connection to the server\n";
    }


    #Login to the testserver.vago.com
    ftp->login("testftp","123qwe") || print "Could not login to the server\n";
    print "Successfull login to the server\n";


    #Get files from the server and then delete them
    $ftp->cwd("/home/testftp/cat/rat");
    @dirlist = $ftp->ls();
    foreach $file (@dirlist) {
    $ftp->get($file,"/home/testftp/cat/rat/$file") ||
    print "Could not get the files";
    notify("Got the file $file from server to the local machine\n");
    print LOG "$tds $file\n";
    $ftp->delete($file) || print "Could not delete $file in server";
    }
      One question on this pretty darn clear example. When you are printing to the LOG, what is $tds ? Cheers

      Kia Kaha, Kia Toa, Kia Manawanui!
      Be Strong, Be Brave, Be perservering!

        I would guess that $tds is a "time/date-stamp", but given that code snippet, I don't know where it would be coming from. I suppose you might want to add a line like:
        my $tds = scalar localtime;
Re: Updating files over multiple servers
by sago (Scribe) on Sep 27, 2007 at 06:05 UTC

    Connect to the server.
    Get the file to the local machine.
    After getting the file to the local machine, delete the file in the server.
    Do what ever you want to update in the file, which is now in the local machine
    Put the updated file on the server.

    This is just a suggesstion or point from my side!!!
      Personally, I wouldn't delete the file on the server until I had the new file ready to ftp back.

      Rather, consider renaming the original server file. This has the added benefits of indicating that the file is being worked on (rather than having been "lost'), giving you something to recover from in case of disaster, and having a touchstone to indicate what changes were made.
      Ok, but how do I connect TO the other servers?

      right now I update .txt files like this:
      open(FI,">>/home/path/to/file.txt") or die "Oops file failed: $!"; seek(FI, 0, 2); print FI "username /specific/path /something else\n"; close(FI);
      So if I have to duplicate everything to the other servers so that no matter which server the load balancer puts the users on, they have that data available to them, how do I use Perl to Open those files, since they are not on the same server.

      Thanks.
      Richard
      Sorry for the confusion in the first question.
        Hello,

        For most of my machines, I simply connect with telnet, i.e. on Unix/Linux machines. Note that I'm in an internal network, so I'm not confronted with many security issues. It all depends on your network, so YMMV.

        There is a perl Telnet module. You can find the documentation here: Net::Telnet. Connect to each machine, open the file, concatenate, close file. The telnet service on Windows is somewhat more difficult. It's also mentioned in the documentation.

        Also, if you search PerlMonks, you're sure to find examples.

        regexes


        -------------------------
        Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan "press on" has solved and always will solve the problems of the human race.
        -- Calvin Coolidge, 30th President of the USA.
Re: Updating files over multiple servers
by dwm042 (Priest) on Sep 27, 2007 at 14:24 UTC
    There are a number of ways to handle this. The commands rsync and rdist have been mentioned. But one easy way to manage files on multiple servers is to keep the files on one server and then NFS mount the directory on all the machines that need these resources. Then your perl scripts can do local writes and the question of remote open commands is moot.

Re: Updating files over multiple servers
by perlofwisdom (Pilgrim) on Sep 27, 2007 at 14:37 UTC
    I use the following code (on Windows) to map network drives to my local machine, then do any file manipulation that I need to as if it were a local drive, and finally disconnect the drive again.
    ###################################################################### # Function: MapNetworkShare # # Description: Map the remote network share to a local drive # # # # Arguments: LOCAL_MOUNT - Local drive letter WITH colon and NO \ # # i.e. I: # # REMOTE_HOST # # REMOTE_SHARE # # REMOTE_USER # # REMOTE_PASS # # # ###################################################################### sub MapNetworkShare { my $LOCAL_MOUNT=shift(@_); my $REMOTE_HOST=shift(@_); my $REMOTE_SHARE=shift(@_); my $REMOTE_USER=shift(@_); my $REMOTE_PASS=shift(@_); # Check for locally mapped drive if ( -d "$LOCAL_MOUNT\\" ) { &UnMapDrive($LOCAL_MOUNT); } # Map remote share $REMOTE_HOST=~s/^\\\\//; #Trim leading \\ if specified &LogMsg ("Mapping remote share \\\\${REMOTE_HOST}\\${REMOTE_SHARE} +as $LOCAL_MOUNT"); $command="net use $LOCAL_MOUNT \\\\${REMOTE_HOST}\\${REMOTE_SHARE}" +; if ($REMOTE_USER ne '' && $REMOTE_PASS ne '') { &LogMsg("Using remote user $REMOTE_USER"); $command.=" /user:${REMOTE_USER} $REMOTE_PASS"; } $out = `$command`; if ( -d "${LOCAL_MOUNT}\\" ) { &LogMsg ("Drive successfully mapped"); return 1; } else { &LogMsg("ORADBA-1047-013: Unable to map drive: $!\n$out"); return($out); } } ###################################################################### # Function: UnMapDrive # # Description: Un-map a local drive if mapped. # # Arguments: The drive (with : but no \) to unmap. # ###################################################################### sub UnMapDrive { my $LOCAL_MOUNT=shift(@_); # Clean up locally mapped drive if ( -d "$LOCAL_MOUNT\\" ) { &LogMsg ("Un-mapping local mount $LOCAL_MOUNT"); $out = `net use ${LOCAL_MOUNT} /del`; if ($? != 0) { &DieMsg("ORADBA-1047-014: Unable to unmount $LOCAL_MOUNT\n${!}$ou +t"); } } else { &LogMsg("Local drive $LOCAL_MOUNT does not exist, unable to unma +p"); } return; }
    Good luck
Re: Updating files over multiple servers
by salva (Canon) on Sep 27, 2007 at 16:56 UTC
    Net::SFTP::Foreign allows you to access remote files as local ones through the SFTP protocol:
    use Net::SFTP::Foreign; use Net::SFTP::Foreign::Constants qw(SSH2_FXF_WRITE SSH_FXF_READ); my $sftp = Net::SFTP::Foreign->new('user@remote-host'); $sftp->error and die "unable to connect to remote host: ".$sftp->error +; my $fh = $sftp->open("remote/file/path", SSH2_FXF_WRITE|SSH2_FXF_READ) or die "unable to open remote file: ". $sftp->error; seek($fh, 0, 2); print $fh "append this line to file\n"; close $fh;