首页 > 系统相关 >使用paramiko上传文件到Linux并且解压

使用paramiko上传文件到Linux并且解压

时间:2023-04-09 23:23:18浏览次数:39  
标签:解压 name zipfile file Linux path paramiko target

思路是,使用zipfile对文件进行压缩,之后使用paramiko提供的SFTPClient()上传文件,之后使用paramiko的SSHClient()执行Linux命令操作文件

import os
import zipfile
import paramiko

# 使用zipfile压缩文件
def zip_code(dir_name, zip_name):
    file_list = []
    if os.path.isfile(dir_name):
        file_list.append(dir_name)
    else:
        for root, dirs, files in os.walk(dir_name):
            for name in files:
                file_list.append(os.path.join(root, name))
    zf = zipfile.ZipFile(zip_name, "w", zipfile.ZIP_DEFLATED)
    for tar in file_list:
        arc_name = tar[len(dir_name):]
        zf.write(tar, arc_name)
    zf.close()

# 使用zipfile解压文件(这是直接在本地解压)
# def unzip_file(zip_file_name, unzip_dir):
#     zf = zipfile.ZipFile(zip_file_name)
#     file_list = zf.namelist()
#     for file in file_list:
#         zf.extract(file, unzip_dir)
#     zf.close()


def upload_code(host, port, username, pwd, local_file, target_path, target_file_name):
    transport = paramiko.Transport((host, port))
    transport.connect(username=username, password=pwd)

    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.put(local_file, target_path + target_file_name)
    client = paramiko.SSHClient()
    client._transport = transport
    cmd_ls = "ls -l " + target_path + target_file_name
    _, stdout, stderr = client.exec_command(cmd_ls)
    if not stdout.read().decode('utf-8'):
        print("失败: ", stderr.read().decode('utf-8'))
        return
    # 这里使用unzip解压,但是zipfile压缩的时候对中文不友好,所以unzip解压需要指定CP936编码
    cmd = "cd " + target_path + " && sudo rm -rf web_code/* && unzip -O CP936 " + target_file_name + " -d web_code/ && rm " + target_file_name
    print("cmd: ", cmd)
    _, stdout, stderr = client.exec_command(cmd)
    print("stdout:", stdout.read().decode('utf-8'))
    print("stderr:", stderr.read().decode('utf-8'))
    print("finish")


def start():
    username = "root"
    pwd = "12345"
    host = "localhost"
    port = 22
    target_file_name = "dist.zip"
    local_file = "./{}".format(target_file_name)
    target_path = "/test/"
    zip_code(r"D:\工作", "./{}".format(target_file_name))
    upload_code(host, port, username, pwd, local_file, target_path, target_file_name)
    input()


if __name__ == '__main__':
    start()

  

标签:解压,name,zipfile,file,Linux,path,paramiko,target
From: https://www.cnblogs.com/hushaojun/p/17301421.html

相关文章

  • Linux 杀毒软件发现的漏洞可使得黑客获得 root 权限
    McAfeeVirusScanEnterprisefor Linux 版本中的远程代码执行缺陷,使黑客可以获得root权限,安全研究人员称只需欺骗该app使用恶意更新服务器即可实现。Linux安全软件发现的漏洞可使得黑客获得Root权限来自MITLincolnLaboratory的AndrewFasano在他的帖子中......
  • Linux 杀毒软件发现的漏洞可使得黑客获得 root 权限
    McAfeeVirusScanEnterprisefor Linux 版本中的远程代码执行缺陷,使黑客可以获得root权限,安全研究人员称只需欺骗该app使用恶意更新服务器即可实现。Linux安全软件发现的漏洞可使得黑客获得Root权限来自MITLincolnLaboratory的AndrewFasano在他的帖子中......
  • Linux awk 命令
    Linuxawk命令这玩意非常重要,是自动化的核心、核心、核心AWK是一种处理文本文件的语言,是一个强大的文本分析工具。之所以叫AWK是因为其取了三位创始人AlfredAho,PeterWeinberger,和BrianKernighan的FamilyName的首字符。语法awk[选项参数]'script'var=value......
  • Linux read 命令
    Linuxread命令Linuxread命令用于从标准输入读取数值。read内部命令被用来从标准输入读取单行数据。这个命令可以用来读取键盘输入,当使用重定向的时候,可以读取文件中的一行数据。语法read[-ers][-aaname][-ddelim][-itext][-nnchars][-Nnchars][-pprompt][-......
  • Linux updatedb 命令
    Linuxupdatedb命令updatedb命令用来创建或更新slocate/locate命令所必需的数据库文件。updatedb命令的执行过程较长,因为在执行时它会遍历整个系统的目录树,并将所有的文件信息写入slocate/locate数据库文件中。注意:slocate本身具有一个数据库,里面存放了系统中文件与目......
  • Linux whereis命令
    Linuxwhereis命令Linux命令大全Linux命令大全Linuxwhereis命令用于查找文件。该指令会在特定目录中查找符合条件的文件。这些文件应属于原始代码、二进制文件,或是帮助文件。该指令只能用于查找二进制文件、源代码文件和man手册页,一般文件的定位需使用locate命令。语法w......
  • linux系统下Hadoop集群环境搭建
    1、前言基于redhat7.6系统搭建全分布式Hadoop3.3.5集群环境(模拟三台机,1主角色,2从角色)。 2、安装虚拟机参考:https://www.cnblogs.com/jpxjx/p/16800831.html 3、虚拟机安装redhat7.6参考:https://www.cnblogs.com/jpxjx/p/16815045.html 4、修改主机名vi/etc/hosts......
  • Linux umask命令
    Linuxumask命令暂时只需要知道有这么个东西Linuxumask命令指定在建立文件时预设的权限掩码。umask可用来设定[权限掩码]。[权限掩码]是由3个八进制的数字所组成,将现有的存取权限减掉权限掩码后,即可产生建立文件时预设的权限。语法umask[-S][权限掩码]参数说明:-S以文......
  • Linux which命令
    Linuxwhich命令Linuxwhich命令用于查找文件。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。语法which[文件...]参数:-n<文件名长度>指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。-p<文件名长度>与-n参数相同,但此处的<文件名长......
  • Linux cp 命令
    Linuxcp命令非常重要,运维必用命令Linuxcp(英文全拼:copyfile)命令主要用于复制文件或目录。语法cp[options]sourcedest或cp[options]source...directory参数说明:-a:此选项通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容。其作用等于dpR参数组......