Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Writing to a file on two different servers

by belize (Deacon)
on Dec 06, 2000 at 03:48 UTC ( [id://45128]=perlquestion: print w/replies, xml ) Need Help??

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

I have a CGI that I would like to have write to a config file on two separate servers. I am using the following:
# Open local file for appending open(OUT,">> $config1) || $Error( "Can't open $config1" ); print OUT "$id|$datestamp|$description\n"; close (OUT); # Open remote file for appending open(OUT,">> $config2) || $Error( "Can't open $config2" ); print OUT "$id|$datestamp|$description\n"; close (OUT);

Where $config1 = "/home/path/to/config1.txt"
and
$config2 = "http://www.differentdomain.com/path/to/confi2.txt"

I get the error:

"Can't open http://www.differentdomain.com/path/to/confi2.txt"

Is there a way to access a file on a remote server for writing?

Replies are listed 'Best First'.
Re: Writing to a file on two different servers
by chipmunk (Parson) on Dec 06, 2000 at 04:00 UTC
    There is no direct way to open a filehandle on a remote server; you would need to open a socket to the remote server, with a process at the other end of the socket cooperating in writing the file. (For example, HTTP works by having your browser open a socket to the remote machine and sending a request; then the web server on the remote machine cooperates by sending back the requested data.)

    The easiest solution in this case is probably to create the configuration file locally, and then upload it to the remote server with Net::FTP.

      OK, alternatively, could I read a config file from a remote server? The reason I ask is that I have program that will run on two different servers, but will use the exact same flat file for data. Instead of having to update the flat file twice, it would be best to update it once and then have both servers read the single flat file.
        You'll still need some cooperation from the remote server to get the file. You could FTP it, scp it, put it on the web and get it with LWP... There are lots of different ways.

        I think what you need to consider first is when is the best time for the configuration file to be transfered; when it changes, or when the scripts run. It may make more sense to distribute the configuration file when it changes, and have the scripts just run on a local copy, so that the script doesn't have to open a remote connection everytime it runs. On the other hand, you might want to avoid changing the file on one server and forgetting to upload it to the other server.

        That decision will, of course, influence how this system is implemented.

        If the files are supposed to be exactly the same, why not creat the file on your current server and then use FTP to copy it to the other one?

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: Writing to a file on two different servers
by marvell (Pilgrim) on Dec 06, 2000 at 16:14 UTC
    There are many ways of solvingh this problem, some of them may be restricted sue to system of network configuration. Some methods are more convenient, but less secure.

    NFS or Samba, etc. Depending on whether or not your machines are of the same type, it may be easy enough have file system shared between the two machine. You may have a problem if the servers are part of some High Availablility system. Having one disk with confif files on makes a single point of failure, which rather defeats the point of HA. If this is the case, some careful cross-mounting will have to be done. Also, some method of recognising that the other server has gone away, before an IO hang is initiated by trying to access a file which is on a mahcine which is no longer in operation. The beauty of this method is that your code does not have to change. The method for opening files remains the same.

    FTPGood old, file transfer. This can be acheived with Net::FTP and makes for a very easy method of transfering file. Obviously, you'll have to write a file, transfer it and then possibly delete it. There are tons of examples of how to do this kicking about. Watch out for such things as file permissions and other oddities, as your FTP server may have some interesting configs which will mean you may have to change things, post transfer.

    HTTPAs your config file is servable from your HTTP server (which is a little insecure), you may wish to transfer the file using this method instead of FTP. The problem you may find here is that you are not able to "PUT" files with your current config. This is, in fact, a good idea if you are serving config file.

    --
    Brother Marvell

Re: Writing to a file on two different servers
by extremely (Priest) on Dec 06, 2000 at 05:02 UTC
    Can you set up NFS or some other file sharing system? Then you could use ">>/mounts/nfs/path/to/file" or "\\machine\path\to\file" or even "G:\path\to\file" depending on system and or method of mounting files.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-24 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found