http://www.perlmonks.org?node_id=866063

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

I am trying to copy a file from one machine to another with a different IP address, using perl. I have used my script to write the new file (on machine A), which is a DOS batch file. Now I would like to xcopy the file over to machine B, where it will be run by the windows scheduler.

Can I use some command to run an external DOS command like xcopy? Or is there a set of commands in perl which are appropriate to the task?

I have begun looking at:


OPENDIR, READDIR, CHDIR, RENAME

Will these work if I supply them with a full path including an IP address?

Replies are listed 'Best First'.
Re: Copying a file across servers in DOS
by Kanji (Parson) on Oct 19, 2010 at 02:07 UTC

    You can run external commands in Perl with system(), exec() or backticks/qx():-

    # Note escaped slashes! system("xcopy dos.bat \\\\machine.b\\share\\")

    You might also find the Shell module useful, as it will allow you to use xcopy as if it were a Perl command:-

    use Shell qw(xcopy); xcopy("dos.bat", "\\\\machine.b\\share\\");

    However, Perl does support UNC-style paths (if your OS does), so you could use plain ol' open() or a more elaborate (robust?) wrapper like File::Copy:-

    open LOCAL, "dos.bat" or die $!; open REMOTE, "//machine.b/share/dos.bat" or die $!; print REMOTE <LOCAL>; use File::Copy; copy("dos.bat", "//machine.b/share/dos.bat") or die $!;

    (Note that the native Perl calls support the alternate use of forwardslashes, which will save you from excessive escaping of UNC paths.)

        --k.


Re: Copying a file across servers in DOS
by kcott (Archbishop) on Oct 18, 2010 at 23:31 UTC

    You have many choices! FTP is a commonly used file transfer protocol. For a Perl solution, have a look at Net::FTP or, if you need a secure data transfer, try Net::SFTP.

    -- Ken

Re: Copying a file across servers in DOS
by tokpela (Chaplain) on Oct 19, 2010 at 16:29 UTC

    As a follow up to Kanji's post, if you need to connect to the remote share in your script, you should look at the Connect (or Map) routines in the Win32::FileOp module.

    The Connect routine will allow you to login to a remote share with a username/password. The Map routine will also allow you to login to a remote share but also give you the ability to map that share as a drive on your local computer.

Re: Copying a file across servers in DOS
by Anonymous Monk on Oct 19, 2010 at 01:17 UTC
    Will these work if I supply them with a full path including an IP address?

    No. Maybe if you have some kind of samba or something and you can cd //ip/f/bar/bar from the commandline

Re: Copying a file across servers in DOS
by aquarium (Curate) on Oct 19, 2010 at 22:06 UTC
    excellent suggestions by the perlmonks.
    an alternative to copying the file in the first place could be to have a perl cgi produce the instruction set when requested, thus presenting it via a web server. then machine 2, or any other machine or machines, simply perform a http get and parse the information into the required schedule and execute. this gets around the whole file permissions and copying mechanism. you could then even extend the perl cgi to be parameter driven, i.e. provide different information depending on parameters passed. if a www server is available or can be easily installed on a machine, it simplifies the whole thing.
    the hardest line to type correctly is: stty erase ^H