Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Copying files using a specific port

by rupesh (Hermit)
on Oct 06, 2003 at 04:15 UTC ( [id://296857]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,
Is there a way of copying files over a specific port using perl? ( for win-32 ).

Considering the fact that you can use a socket-server connection and connect to the server (in a differrent machine) using a pre-defined port, is there a way to perform a copy operation using the same concept?

Something like ... (for instance ) open port 1234 in machine 1 and machine 2 and send a stream of files from mac. 1 to mac. 2 .....

Any ideas?
Thanks!

Ascii Effect

Replies are listed 'Best First'.
Re: Copying files using a specific port
by waswas-fng (Curate) on Oct 06, 2003 at 04:38 UTC
    Rsync,ftp,sftp,scp,mount a drive using smb/afp then use the the File::Copy. There are many many ways you can do this what are you actually trying to do?


    -Waswas
      Thanks for the quick response,
      I don't wan't to use the other methods suggested such as FTP, mount etc., and would like to use purely perl script(s).

      Here's what Im trying to do:
      Execute a perl server script on one machine, which listens to all clients from a specific port. Connect to the server using a socket script via the predefined port. NOw, that the connection is established using the socket-server, use the same script to copy files from the client to the server over the same port.

         Well the short answer is "Yes" it is possible to create code which does this.

         I think that most people would question why you would wish to do this though, there are many tools which solve this problem already which have been mentioned above.

         If you have some specific problem with them then you should probably mention it - instead of looking to reinvent the wheel.

         For example you might have a problem with entering passwords - in which case you should investigate the use of shared keys and SSH, making passwords unnecessary.

         Or if you do not have root access to the machine you wish to copy files to, and cannot install or modify software there then that might be a valid vetoing option. However you may be able to do things in reverse in that case, instead of connecting to the machine connect from there and "pull" the files.

         The perl code to do the copying is going to open up a whole can of worms:

        • You will need to create a protocol to specify the password (you are going to password protect this, right?) to allow the copy to proceed.
        • You will have to pass the name of the file you wish the receiving side to write to.
        • You probably want to "lock" the sender into a particular directory, so they don't write to ~/.ssh/authorized_keys, or /etc/password for example.
        • You'll need to deal with errors writing and signal them back to the client.

         Those are just a small list of things that occur to me.

         However if you really want to do this, and it's not exposed to the internet you should start by looking at IO::Socket::INET, File::Copy and related modules.

        Steve
        ---
        steve.org.uk
Re: Copying files using a specific port
by sgifford (Prior) on Oct 06, 2003 at 16:22 UTC

    There are two things you need your client to tell your server: the file's name and the file's contents. Part of the contents is knowing when the file ends, so either you have to send a length at the beginning, some kind of EOF marker at the end, or else close the connection at EOF.

    A simple protocol would be:

    > SENDFILE <size> name<eol>
    < OK
    > (file contents, exactly <size> bytes)
    < OK
    
    Assuming you have the file name and size in variables already, this could be as simple as:
    print SOCK "SENDFILE $filesize $filename\r\n"; $resp = <SOCK>; if ($resp !~ /^OK/) { die "Bad response\n" } while(read(INFILE,$_,8192)) { print SOCK; } $resp = <SOCK>; if ($resp !~ /^OK/) { die "Bad response\n" }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found