前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >file_get_put.py

file_get_put.py

原创
作者头像
不凡
发布2022-03-10 09:37:57
4260
发布2022-03-10 09:37:57
举报
文章被收录于专栏:运维小郭运维小郭运维小郭
import os
import paramiko
from scp import SCPClient


class ScpClient_intface:
    __host = "10.111.3.50"
    __port = 22
    __username = "root"
    __password = "111111"
    file_name = "e.sh"
    remote_path = "/root/test1/"
    local_path = "D:\test"
    def set_scp_server_information(self,host_ip="10.111.3.50",port = 22,username ="root",password = "111111"):
        self.__host = host_ip
        self.__port = port
        self.__username =username
        self.__password = password
    def get_file_from_scp_server(self,file_name,remote_path="/root/test1",local_path="D:\test"):
        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
        # local_path = file_path + "\\" + img_name
        print(file_path_lo)
        print(file_path_re)
        try:
           # scpclient.put(local_path,remote_path) #上传到服务器上
            scpclient.get(file_path_re, file_path_lo)
        except FileNotFoundError as e:
            print(e)
            print("system  file not found" + local_path)
            result = "system  file not found" + 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="/root/test1/",local_path="D:\test"):
        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)
        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)
            #scplient.get(file_path_re,file_path_lo)
        except FileNotFoundError as e:
            print(e)
            print("system file not found" + local_path)
            result = "system file not found" + local_path
        else:
            print("File upload successfully")
            result = "File upload 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_server("e.sh",local_path=os.getcwd())
    #scp_cc.put_file_to_scp_service("www.txt",local_path=os.getcwd())

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档