【问题标题】:Read files on remote Windows machine using Python Paramiko使用 Python Paramiko 读取远程 Windows 机器上的文件
【发布时间】:2020-06-08 01:41:43
【问题描述】:

我需要从 Linux 读取和处理远程 Windows 机器上的文件。我可以设置 SSH 会话并使用 Python Paramiko 运行一些简单的命令,但我无法弄清楚读取远程文件的方法。远程文件是一个日志,我希望在每次执行循环时读取和处理一些行。感谢有人可以分享您的想法或代码。

【问题讨论】:

    标签: python windows ssh paramiko


    【解决方案1】:

    对于通过 SSH 连接操作远程文件,不要使用 shell 命令,使用标准 SSH 文件管理 API,SFTP

    ssh = paramiko.SSHClient()
    # ...
    ssh.connect(...)
    
    sftp = ssh.open_sftp()
    
    with sftp.open("/remote/path/my.log", "r") as f:
        for line in f:
            # process the line as you need
            print(line.rstrip())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2017-09-09
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多