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

I am working on a script to transfer application code to multiple servers. I have actually got the Net::FTP::Recursive working using an 'rput' to transfer the code. The problem I am running into is I want to preserve the permission and ownership, however, these are NOT being preserved. The ownership is changing to the ftp account used for the transfer, and the permissions are being changed to 640 for files and 750 for directories.

I am afraid that this is a very stupid question, but I am under the gun on time for this and really need to get rolling again. Thanks in advance. What I am really hoping for is a transfer mode or switch to preserve file permissions and ownership. Anyone know of if such a beast exists?

  • Comment on Net::FTP::Recursive Permission and Ownership...

Replies are listed 'Best First'.
Re: Net::FTP::Recursive Permission and Ownership...
by Elijah (Hermit) on Apr 05, 2004 at 20:47 UTC
    What platform are you using this on? I am going to assume Linux? As far as the permissions go these settings are probably the default settings for new files and directories for the systems themselves. You can change the default settings to match what you want each new file and directory to reflect or simply change the permissions upon upload completion.

    Ex: Untested!

    #!/usr/bin/perl -w use strict; use Net::FTP; my $ftp_login = "login"; my $ftp_pass = "password"; my $file = "/full/file/path/to/code.tar"; my $chmod = "chmod 777 $file"; my $chown = "chown user $file"; my @list = qw[server1 server2 server3 server4 server5]; for (@list) { if (my $ftp = Net::FTP->new($_)) { print "FTP object is: $ftp\n"; print "\nFTP session for $_ is active!\n"; print "Logging in ..."; if ($ftp->login($ftp_login,$ftp_pass)) { print "Done!\n\n"; $ftp->binary(); $ftp->put($file); $ftp->cmd($chmod); $ftp->cmd($chown); print "Upload complete and permissions set!\n"; }else{ print "\nCould not log in!\n"; $ftp->quit(); } }else{ print "A connection to $_ was not made!\n"; } }
    www.perlskripts.com
Re: Net::FTP::Recursive Permission and Ownership...
by insensate (Hermit) on Apr 06, 2004 at 15:57 UTC
    From the libnet FAQ:
    The FTP protocol does not have a command for changing the permissions of a file on the remote server. But some ftp servers may allow a chmod command to be issued via a SITE command, eg $ftp->quot('site chmod 0777',$filename); But this is not guaranteed to work.
    Programmatically, I think you're going to hit a world of trouble trying to accomplish what you're trying to do... A hacky workaround would be to use Net::Telnet to go in after the files are at the remote site and issue the changes you desire... but that doesn't surmount the fact that most secure servers don't let users without superuser privledges chown files.

    What is preventing you from logging into the remote host as the user you ultimatly want to own the files?

      The general idea here is that the group I am currently in is being dumped on to push application code. I am working on some scripts to allow another group to actually perform this code push.

      Currently the process is to tar | remsh to files to each remote system. This requires root access, and some knowledge. I am trying to script an idiot proof method of performing this same thing. I am trying to use the Net::FTP::Recursive module to get the files over. I am beginning to wonder though if I my be better served in simply making a 'system' call to do the same tar|remsh method that we currently do. I have been trying to avoid this because I thought the ftp method would be more secure and possibly faster too.

      Once again, any suggestions/ideas are most appreciated. Thanks.

        I don't see why the process you describe requires root access... I think Net:FTP::Recursive is the perfect tool. If you have the access to do the remsh (I assume now you're on HP-UX) you should have the same access to the account you want owning the files on the remote system(s). As for changing the file permissions, that can be a simple script after you put the files out... using Net:SSH would be ideal. Avoid chmod -R though for obvious security reasons... I would instead build a list of files... maybe with the File::Find modules so you can chmod specific filenames/directories.
        use File::Find; sub process_file { push @files, $File::Find::name unless (-d); push @dirs, $File::Find::name if (-d) } find( \&process_file, "your src dir" );