python paramiko+scpclient SCP遠程文件拷貝應用

image.png

git鏈接:https://github.com/monkeyish-smart/python-paramiko-scpclient.git
1、實現(xiàn)scp協(xié)議的遠程文件拷貝,注意:不是FTP 也不是SFTP。
2、為什么要做scp, 因為scp使用ssh 對資源有限的嵌入式linux 比較常用
3、scp文件拷貝使用paramiko 和 SCPClient庫開發(fā)
4、gui開發(fā)使用tkinter庫
5、本人第一個python 程序 mark 下

廢話不多說上代碼:
ScpClient_intface.py 文件拷貝操作

import paramiko  # 用于調(diào)用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 頁面與回調(diào)接口

#!/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='zhanyongli@xcharge.com', fg='blue', font=('Arial', 10)).pack()
if __name__ == "__main__":
    # 第1步,實例化object,建立窗口window
    root = tk.Tk()   # 創(chuàng)建窗口對象的背景色
    # 第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)
    #創(chuàng)建下拉菜單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()

調(diào)試時候有個問題 我是用PyCharm ide 開發(fā)的,剛開始下載的是scpclient庫會提示缺庫 搞了好久才發(fā)現(xiàn)應該下載scp庫使用,希望大家能注意下。

image.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容