如何通过python从windows上传文件到linux指定目录

实现方案

pycharm新建文件upload_file.py,运行即可

upload_file.py文件内容如下

# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import paramiko
import datetime
import os


hostname = '***.***.***.**' #upload IP addr
username = '****'
password = '****'
port = 22


def upload(local_dir, remote_dir):
    try:
        t = paramiko.Transport((hostname, port))
        t.connect(username=username, password=password)
        sftp = paramiko.SFTPClient.from_transport(t)
        print('upload file start %s ' % datetime.datetime.now())
        for root, dirs, files in os.walk(local_dir):
            print('[%s][%s][%s]' % (root, dirs, files))
            for filespath in files:
                local_file = os.path.join(root, filespath)
                print(11, '[%s][%s][%s][%s]' % (root, filespath, local_file, local_dir))
                a = local_file.replace(local_dir, '').replace('\\', '/').lstrip('/')
                print('01', a, '[%s]' % remote_dir)
                remote_file = os.path.join(remote_dir, a)
                print(22, remote_file)
                try:
                    sftp.put(local_file, remote_file)
                except Exception as e:
                    sftp.mkdir(os.path.split(remote_file)[0])
                    sftp.put(local_file, remote_file)
                    print("66 upload %s to remote %s" % (local_file, remote_file))
            for name in dirs:
                local_path = os.path.join(root, name)
                print(0, local_path, local_dir)
                a = local_path.replace(local_dir, '').replace('\\', '')
                print(1, a)
                print(1, remote_dir)
                remote_path = os.path.join(remote_dir, a)
                print(33, remote_path)
                try:
                    sftp.mkdir(remote_path)
                    print(44, "mkdir path %s" % remote_path)
                except Exception as e:
                    print(55, e)
        print('77,upload file success %s ' % datetime.datetime.now())
        t.close()
    except Exception as e:
        print(88, e)


if __name__ == '__main__':
    local_dir = r'D:\Tmp'
    remote_dir = '/opt/'
    upload(local_dir, remote_dir)

附:

运行(python3.7.2)如产生问题:ModuleNotFoundError: No module named 'paramiko',

解决方案为在terminal (pycharm)中进行paramiko安装

(如需要查看当前Python路径 ,terminal 输入  where python )

pip install paramiko

安装完成后即可正常运行


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Python 中从 Windows 主机拷贝文件Linux 主机,你可以使用 Paramiko 库来实现 SSH 连接和文件输。以下是一个示例代码: ```python import paramiko def copy_file_windows_to_linux(source_file, destination_file, windows_host, windows_user, windows_password, linux_host, linux_user, linux_password): # 创建 SSH 客户端 ssh_client = paramiko.SSHClient() ssh_client.load_system_host_keys() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # 连接 Windows 主机 ssh_client.connect(windows_host, username=windows_user, password=windows_password) # 使用 SFTP 进行文件输 sftp = ssh_client.open_sftp() # 从 Windows 主机下载文件到本地 sftp.get(source_file, destination_file) # 关闭 SFTP 连接 sftp.close() # 关闭 SSH 连接 ssh_client.close() # 创建 SSH 客户端 ssh_client = paramiko.SSHClient() ssh_client.load_system_host_keys() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # 连接 Linux 主机 ssh_client.connect(linux_host, username=linux_user, password=linux_password) # 使用 SFTP 进行文件输 sftp = ssh_client.open_sftp() # 将文件Linux 主机 sftp.put(destination_file, destination_file) # 关闭 SFTP 连接 sftp.close() # 关闭 SSH 连接 ssh_client.close() print("文件Windows 主机成功复制到 Linux 主机!") except Exception as e: print("连接或文件Linux 主机时出现错误:", str(e)) except Exception as e: print("连接或文件下载到 Windows 主机时出现错误:", str(e)) # 调用函数进行文件拷贝 copy_file_windows_to_linux('C:\\path\\to\\file.txt', '/path/on/linux/file.txt', 'windows_host', 'windows_user', 'windows_password', 'linux_host', 'linux_user', 'linux_password') ``` 在上面的代码中,你需要将 `'C:\\path\\to\\file.txt'` 替换为实际的 Windows 文件路径,`'/path/on/linux/file.txt'` 替换为实际的 Linux 目标路径,以及 `'windows_host'`、`'windows_user'`、`'windows_password'`、`'linux_host'`、`'linux_user'` 和 `'linux_password'` 替换为实际的主机和凭据信息。 这段代码使用 Paramiko 库创建 SSH 连接,然后使用 SFTP 进行文件输。首先,它从 Windows 主机下载文件到控制节点,然后将文件Linux 主机。最后,它在控制台打印成功消息或错误消息。 确保在运行代码之前已经安装了 Paramiko 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值