简体   繁体   中英

download/upload files to remote windows server using python

I want to download/upload file from remote windows 2008 R2 servers using my python script. The problem is I do not want to install anything extra on my windows server box. I want to achieve this just using my normal login credentials.

Below are the different methods I heard of:

  1. Use paramiko SSH : But to use this we have to install an SSH service on the remote box, which i do not want to do.
  2. Use python wmi module: But I guess it does not have the functionality to download files from the remote servers.
  3. Mount drives on your local box: Also do not want to do this as there will be lot of machines i want to connect to.
  4. Use winscp: I guess it will also require SSH ?
  5. Fabric: Heard of this, not sure what are its prerequisites.

are there any other methods with which i can achieve this?

When in windows do as the windows-users do.

If you can't install additional software on the server, you need to mount the drive, and interact with the remote files like they are local files.

You mention that you have too many remote servers to connect to. Why not pick one drive letter, and reuse it for each server you need to connect to?

With net use you can mount from the command line.

Syntax for net use

net use p: /delete:yes
net use p: \\remote_host\share_name password /user:domain\user

Use Python's subprocess package to run the mount commands. Subprocess tutor .

import subprocess

# Make sure the drive isn't mounted.
try:
    subprocess.call('net use p: /delete:yes', shell=True)
except:
    # This might fail if the drive wasn't in use.
    # As long as the next net use works, we're good.
    pass

for host in ("host1", "host2"):
    # mount(map) the remote share.
    subprocess.call('net use p: \\%s\share_name password /user:domain\user' % host, shell=True)
    with open("p:\path\remote-file.txt", "r") as remote_file:
        # do stuff
    # dismount(map) the drive
    subprocess.call('net use p: /delete:yes', shell=True)

(Don't have a windows box and network to test this on.)

Use the win_unc library: http://covenanteyes.github.io/py_win_unc/

This will allow you to do normal modifications to file paths as well as log in as a different user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM