Copying files to multiple comp's on a lan

David Bolen db3l at fitlinxx.com
Fri Jun 1 15:35:37 EDT 2001


"Henrik Berg Nielsen" <hbn at imada.sdu.dk> writes:

> I need to make a small script thats capable of copying a file from the
> machine that its being run on to all the other machines on the lan. The
> target path on all the other machines should be the same. The script should
> take the path of the source file and the remote path to the shared folder(s)
> as arguments. I'll define all the machines the file is to be copied to
> elsewhere.
(...)
> I used to do this filecopying for each machine seperately with the Windows
> Explorer, and seeing as its something that I do rather often, I'm beginning
> to get a little bugged by this tedious method, and have decided that now its
> time to make it a little more automated.
> If you know of an already existing program that can accomplish this task for
> me any pointers would be greatly appreciated.

Since you mention Windows, I'd probably a Windows specific method
since you can ask Windows to identify the machines on the LAN, and use
a Win32 API call to perform the copy which in general will outperform
any other script based approach.

Here's a function from one of our upgrade scripts that replicates a
distribution tree either locally or to a specific machine.  In this
case it assumes the source tree mimics the target tree off of the
administrative share for C$ but that's easy enough to adjust or
parameterize:

    import os, win32file

    def dist_files(machine, dist_root, filelist):
	"""Replicates a set of files to the target machine.  It is assumed
	that the filelist is relative to C: on the target machine"""

	if machine:
	    root = r'\\%s\c$' % machine
	else:
	    root = 'c:'

	for file in filelist:
	    relative = os.path.normpath(os.path.splitdrive(file)[1])
	    if relative[0] == os.sep:
		relative = relative[1:]
	    source = dist_root + '\\' + relative
	    target = root + '\\' + relative

	    print '  ', source
	    subdir = os.path.dirname(relative)
	    check_directory(root,subdir)
	    # Even if told not to complain if file exists, CopyFile will fail
	    # if the file is R/O, so we always try to reset that bit
	    try:
		os.chmod(target,0777)
	    except:
		pass
	    win32file.CopyFile(source,target,0)

This is designed such that the source tree mirrors the target location, thus
you might make a source tree like:

    C:\DIST\
	   \DIR1
		\file1.txt
	   \DIR2
		\file2.txt

and assuming that for the target, C:\DIST should be considered
equivalent to C:\, you could distribute this with:

    dist_files(<machine>,r'c:\dist',[r'dir1\file1.txt',r'dir2\file2.txt'])

or if you are just copying from the true file location on the source
(e.g., C: on source is C: on dest):

    dist_files(<machine>,r'c:',[filelist])

and of course you can automate the construction of the filelist if you
like (os.listdir, glob.glob, etc...)

Now, in order to determine what machines exist on the LAN, you can use
code like this (the win32net call doesn't work on Win9x - you'd have
to revert to a win32wnet approach):

    import win32api, win32net

    mymachine = win32api.GetComputerName()
    machlist = win32net.NetServerEnum(None,100)
    machines = []
    for curmachine in machlist[0]:
	name = str(curmachine['name'])
	if name != mymachine:
	    machines.append(name)
    # At this point machines is a list of machines other than myself


--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list