使用python下载/上传文件到远程windows服务器

使用python下载/上传文件到远程windows服务器

问题描述:

我想使用我的python脚本从远程windows 2008 R2服务器下载/上传文件。问题是我不想在我的Windows服务器上安装任何额外的东西。我想用我的正常登录凭据来实现这一点。使用python下载/上传文件到远程windows服务器

下面是不同的方法,我听到的:

  1. 使用的paramiko SSH:但使用此,我们必须对遥控盒,这是我不想做安装SSH服务。
  2. 使用python wmi模块:但我想它没有从远程服务器下载文件的功能。
  3. 在您的本地盒子上挂载驱动器:也不想这样做,因为会有很多我想连接的机器。
  4. 使用winscp:我想它也需要SSH?
  5. 面料:听说过,不知道它的先决条件是什么。

有没有其他方法可以实现这个目标?

+0

为什么不直接使用UNC路径?只要您的帐户有权限,只需写入:\\ server_name \ $ [drive_letter] \ etc .. – 2015-03-30 17:50:17

+0

除非您映射d驱动器,否则您将如何从您的本地Windows机器使用它? – Pankaj 2015-03-30 18:01:17

在windows中的时候像windows用户那样做。

如果您无法在服务器上安装其他软件,则需要安装驱动器,并与远程文件(如本地文件)进行交互。

您提到您要连接的远程服务器太多。为什么不选择一个驱动器号,并为需要连接的每台服务器重新使用它?

使用net use您可以从命令行进行挂载。

Syntax for net use

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

使用Python的subprocess包运行mount命令。 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) 

(不要有窗户框和网络测试这个上。)

使用win_unc库:http://covenanteyes.github.io/py_win_unc/

这将允许你做正常的修改文件路径以及以不同的用户身份登录。