python scp 传输文件 watchdog监控文件变动

1.本地到远程

scp <本地文件> <远程用户名>:<远程ip>:<远程文件夹目录>

scp /home/aiuser/workspace/detection/PaddleDetection/output/ppyolo_r50vd_dcn_voc_2021_7_30_bf.zip aiuser@192.168.1.100:/home/aiuser/workspace/detection/PaddleDetection/output

2.远程到本地

scp <远程用户名>:<远程ip>:<远程文件夹目录文件> <本地文件夹>

scp aiuser@192.168.1.100:/home/aiuser/workspace/detection/PaddleDetection/output /home/aiuser/workspace/detection/PaddleDetection/output/ppyolo_r50vd_dcn_voc_2021_7_30_bf.zip

3.外网传输

scp -P <外网端口> aiuser@<外网ip>:/home/aiuser/workspace/detection/PaddleDetection/output /home/aiuser/workspace/detection/PaddleDetection/output/ppyolo_r50vd_dcn_voc_2021_7_30_bf.zip

4.代码

1 需要输入密码

import os
os.system("scp -P <外网端口> aiuser@<外网ip>:/home/aiuser/workspace/detection/PaddleDetection/output /home/aiuser/workspace/detection/PaddleDetection/output/ppyolo_r50vd_dcn_voc_2021_7_30_bf.zip")

2 不需要输入密码

# 将指定目录的图片文件上传到服务器指定目录
# remote_path远程服务器目录
# file_path本地文件夹路径
# img_name是file_path本地文件夹路径下面的文件名称
def upload_img(remote_path="/home/aiuser/mtl/workspace/image/", file_path="/home/aiuser/mtl/workspace/image/1.jpg"):
    try:
   		host = ""  # 服务器ip地址
        port = 2021  # 端口号
        username = "user"  # ssh 用户名
        password = "password"  # 密码

        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh_client.connect(host, port, username, password)
        scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)
        p = "/".join(file_path.split("/")[8:])
        scpclient.put(file_path, os.path.join(remote_path,p))
    except FileNotFoundError as e:
        print(e)
        print("系统找不到指定文件" + file_path)
        ssh_client.close()
    else:
        print("文件上传成功")

5.监控本地文件使用scp同步

#coding=utf-8
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time    : 2021/08/24
# @Author  : mtl
# @Desc    : 图片同步
# @File    : sync_server_file.py
# @Software: PyCharm

import time
import argparse
import os
import paramiko  # 用于调用scp命令
from scp import SCPClient

from watchdog.observers import Observer
from watchdog.events import *


host = "<ip>"  # 服务器ip地址
port = <端口>  # 端口号
username = "<用户名>"  # ssh 用户名
password = "<密码>"  # 密码

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(host, port, username, password)
scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)

# 将指定目录的图片文件上传到服务器指定目录
# remote_path远程服务器目录
# file_path本地文件夹路径
# img_name是file_path本地文件夹路径下面的文件名称
def upload_img(remote_path="/home/aiuser/mtl/workspace/image/", file_path=""):
    try:
        p = "/".join(file_path.split("/")[8:])
        scpclient.put(file_path, os.path.join(remote_path,p))
    except FileNotFoundError as e:
        print(e)
        print("系统找不到指定文件" + file_path)
        ssh_client.close()
    else:
        print("文件上传成功")



class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print("文件被修改了 %s" % event.src_path)

    def on_created(self, event):
        print("文件被创建了 %s" % event.src_path)
        if ".jpg" in event.src_path:
            upload_img(file_path=event.src_path)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--path", default=r"/home/aiuser/cover/code/data/image/test_img", help="目录位置")
    args = parser.parse_args()
    if args.path == "":
        raise ValueError("参数为空")

    path = args.path
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        observer.stop()
    observer.join()
python3 sync_server_file.py --path=<监控地址>