【问题标题】:download/upload files to remote windows server using python使用 python 将文件下载/上传到远程 Windows 服务器
【发布时间】:2015-06-03 20:35:49
【问题描述】:

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

以下是我听说过的不同方法:

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

还有其他方法可以实现吗?

【问题讨论】:

  • 为什么不直接使用UNC路径呢?只要您的帐户有权限,只需写入:\\server_name\$[drive_letter]\ etc..
  • 除非你映射驱动器,否则你将如何在本地 Windows 机器上使用它?

标签: python windows ssh paramiko


【解决方案1】:

在 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 包运行挂载命令。 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)

(没有 windows 框和网络来测试它。)

【讨论】:

    【解决方案2】:

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

    这将允许您对文件路径进行正常修改以及以其他用户身份登录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      相关资源
      最近更新 更多