python paramiko+scpclient SCP远程文件拷贝应用

image.png

git链接:https://github.com/monkeyish-smart/python-paramiko-scpclient.git
1、实现scp协议的远程文件拷贝,注意:不是FTP 也不是SFTP。
2、为什么要做scp, 因为scp使用ssh 对资源有限的嵌入式linux 比较常用
3、scp文件拷贝使用paramiko 和 SCPClient库开发
4、gui开发使用tkinter库
5、本人第一个python 程序 mark 下

废话不多说上代码:
ScpClient_intface.py 文件拷贝操作

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

class ScpClient_intface:
    __host = "192.168.0.232"  # 服务器ip地址
    __port = 22  # 端口号
    __username = "root"  # ssh 用户名
    __password = "root"  # 密码
    file_name = "C6SE_project.log" # 操作的文件明
    remote_path = "/A8/"
    local_path = "D:\python_eg"
    def set_scp_server_information(self,host_ip="192.168.0.232",port = 22,username = "root",password = "root"):
        self.__host = host_ip
        self.__port = port
        self.__username = username
        self.__password = password
    def get_file_from_scp_service(self,file_name, remote_path="/A8/",  local_path="D:\python_eg"):
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh_client.connect(self.__host, self.__port, self.__username, self.__password)
        scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)
        # local_path = file_path + "\\" + img_name
        file_path_lo = local_path
        file_path_re = remote_path + '/' + file_name
        #file_path_re = remote_path

        print(file_path_lo)
        print(file_path_re)
        try:

           #  print(local_path)
           # scpclient.put(localpath, remotepath)  # 上传到服务器指定文件
            scpclient.get(file_path_re, file_path_lo)  # 从服务器中获取文件
        except FileNotFoundError as e:
            print(e)
            print("system could not find the specified file" + local_path)
            result = "system could not find the specified file" + local_path
        else:
            print("File downloaded successfully")
            result ="File downloaded successfully"
        ssh_client.close()
        return result
    def put_file_to_scp_service(self,file_name, remote_path="/A8/",  local_path="D:\python_eg"):
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh_client.connect(self.__host, self.__port, self.__username, self.__password)
        scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)
       # local_path = file_path + "\\" + img_name
        file_path_lo = local_path + '/' + file_name
        file_path_re = remote_path

        print(file_path_lo)
        print(file_path_re)
        try:
           scpclient.put(file_path_lo, file_path_re)  # 上传到服务器指定文件
           #scpclient.get(file_path_re, file_path_lo)  # 从服务器中获取文件
        except FileNotFoundError as e:
            print(e)
            print("system could not find the specified file" + local_path)
            result = "system could not find the specified file" + local_path
        else:
            print("文件上传成功")
            result = "File uploaded successfully"
        ssh_client.close()
        return result
if __name__ == "__main__":
    #upload_img("C6SE")
    #print(os.getcwd())
    scp_cc = ScpClient_intface()
    scp_cc.set_scp_server_information()
    scp_cc.get_file_from_scp_service("C6SE_project.log",local_path=os.getcwd() )
    #scp_cc.put_file_to_scp_service("ScpClient_intface.py",local_path=os.getcwd())

scp_client_file_cmd.py 页面与回调接口

#!/usr/bin/python3
#-*- encoding=UTF-8 -*-
import tkinter as tk
import ScpClient_intface as sci
import os
def scp_put_file():
    print(En_host.get(),En_port.get(),En_username.get(),En_password.get(),En_file_name_put.get(),En_remote_path_put.get())
    if En_username.get() == "" or En_password.get() == "":
        scp_put = sci.ScpClient_intface()
        scp_put.set_scp_server_information( host_ip=En_host.get(), port = En_port.get())
        result = scp_put.put_file_to_scp_service(En_file_name_put.get(), remote_path=En_remote_path_put.get(),local_path=os.getcwd())
    else:
        scp_put = sci.ScpClient_intface()
        scp_put.set_scp_server_information( host_ip=En_host.get(), port = En_port.get(), username = En_username.get(), password = En_password.get())
        result = scp_put.put_file_to_scp_service(En_file_name_put.get(), remote_path=En_remote_path_put.get(),local_path=os.getcwd())
    root1 = tk.Tk()
    root1.title('result')
    root1.geometry('200x100')  # 这里的乘是小x
    tk.Label(root1, text=result, fg='blue', font=('Arial', 10)).pack()
    #scp_put = sci.ScpClient_intface()
    #scp_put.set_scp_server_information()
    #scp_put.get_file_from_scp_service("C6SE_project.log", local_path=os.getcwd())
    #scp_put.put_file_to_scp_service("ScpClient_intface.py", local_path=os.getcwd())
def scp_get_file():
    print(En_host.get(), En_port.get(), En_username.get(), En_password.get(), En_file_name_get.get(), En_remote_path_get.get())
    if En_username.get() == "" or En_password.get() == "":
        scp_get = sci.ScpClient_intface()
        scp_get.set_scp_server_information(host_ip=En_host.get(), port=En_port.get())
        result = scp_get.get_file_from_scp_service(En_file_name_get.get(), remote_path=En_remote_path_get.get(), local_path=os.getcwd())
    else:
        scp_get = sci.ScpClient_intface()
        scp_get.set_scp_server_information(host_ip=En_host.get(), port=En_port.get(), username=En_username.get(),password=En_password.get())
        result = scp_get.get_file_from_scp_service(En_file_name_get.get(), remote_path=En_remote_path_get.get(),local_path=os.getcwd())
    root1 = tk.Tk()
    root1.title('result')
    root1.geometry('200x100')  # 这里的乘是小x
    tk.Label(root1, text=result, fg='blue', font=('Arial', 10)).pack()
def about():
    root1 = tk.Tk()
    root1.title('about')
    root1.geometry('200x100')  # 这里的乘是小x
    tk.Label(root1, text='versions 1.0', fg='blue', font=('Arial', 10)).pack()
    tk.Label(root1, text='[email protected]', fg='blue', font=('Arial', 10)).pack()
if __name__ == "__main__":
    # 第1步,实例化object,建立窗口window
    root = tk.Tk()   # 创建窗口对象的背景色
    # 第2步,给窗口的可视化起名字
    root.title('SCP client for file operation')
    # 第3步,设定窗口的大小(长 * 宽)
    root.geometry('520x220')  # 这里的乘是小x
    tk.Label(root, text='Host name(ip)', font=('Arial', 10)).place(x=10, y=10)
    tk.Label(root, text='port', font=('Arial', 10)).place(x=150, y=10)
    tk.Label(root, text='User name:', font=('Arial', 10)).place(x=250, y=10)
    tk.Label(root, text='Password:', font=('Arial', 10)).place(x=400, y=10)
    # 第4步,在图形界面上设定输入框控件entry并放置控件
    En_host = tk.StringVar()
    En_port = tk.StringVar()
    En_username = tk.StringVar()
    En_password = tk.StringVar()
    En_file_name_get = tk.StringVar()
    En_remote_path_get = tk.StringVar()
    En_file_name_put = tk.StringVar()
    En_remote_path_put = tk.StringVar()
    tk.Entry(root, textvariable = En_host,show=None,  font=('Arial', 10)).place(x=10, y=30,width= 120) # 显示成密文形式
    tk.Entry(root, textvariable = En_port, show=None, font=('Arial', 10)).place(x=150, y=30,width= 50) # 显示成明文形式
    tk.Entry(root, textvariable = En_username, show=None,  font=('Arial', 10)).place(x=250, y=30,width= 100)  # 显示成密文形式
    tk.Entry(root, textvariable = En_password, show='*', font=('Arial', 10)).place(x=400, y=30,width= 100)  # 显示成明文形式
    En_port.set("22")
    tk.Label(root, text='Remote Directory:', font=('Arial', 10)).place(x=10, y=70)
    tk.Label(root, text='File name:', font=('Arial', 10)).place(x=10, y=100)
    tk.Label(root, text='Remote Directory:', font=('Arial', 10)).place(x=10, y=150)
    tk.Label(root, text='File name:', font=('Arial', 10)).place(x=10, y=180)

    tk.Entry(root, textvariable = En_remote_path_put, show=None,  font=('Arial', 10)).place(x=150, y=70,width= 300) # 显示成密文形式
    tk.Entry(root, textvariable = En_file_name_put, show=None, font=('Arial', 10)).place(x=150, y=100,width= 300) # 显示成明文形式
    tk.Entry(root, textvariable = En_remote_path_get, show=None,  font=('Arial', 10)).place(x=150, y=150,width= 300)  # 显示成密文形式
    tk.Entry(root, textvariable = En_file_name_get, show=None, font=('Arial', 10)).place(x=150, y=180,width= 300)  # 显示成明文形式

    tk.Button(root, text='put', anchor='c', width=6, height=1, command=scp_put_file).place(x=450, y=80,height=50,width= 50)
    tk.Button(root, text='get', anchor='c', width=6, height=1, command=scp_get_file).place(x=450, y=150,height=50,width= 50)

    menubar = tk.Menu(root)
    #创建下拉菜单Help
    helpmenu = tk.Menu(menubar, tearoff=0)
    helpmenu.add_command(label="About", command=about)
    menubar.add_cascade(label="Help", menu=helpmenu)
    #显示菜单
    root.config(menu=menubar)
    root.mainloop()

调试时候有个问题 我是用PyCharm ide 开发的,刚开始下载的是scpclient库会提示缺库 搞了好久才发现应该下载scp库使用,希望大家能注意下。

image.png

你可能感兴趣的:(python paramiko+scpclient SCP远程文件拷贝应用)