根据时间建立文件,存储到linux中,再将linux下的几个目录根据时间名来打包下载到本地



1. 在当前python项目目录下建立work目录,并在其中建立同名文本文档work.txt,在txt文件中写入
当前日期的月和日便可
2. 链接虚拟机,将刚才写好的work.txt上传到虚拟机/home/目录下,同时将本地的work.txt删除。
3. Linuxetc目录下的passwd,shadow,group三个文件使用tar命令进行打包,包名为work.txt的内容加上文件的首字母,例如:0725p.tar.gz0725s.tar.gz0725g.tar.gz,将压缩后的三个
文件下载到本地work目录
'''
import os
import paramiko
import datetime
import shutil

class linux():

    def __init__(self,ip,port,user,password,workpath,linuxpath):
        self.ip=ip
        self.port=port
        self.user=user
        self.password=password
        self.workpath=workpath
        self.linuxpath=linuxpath


#1. 在当前python项目目录下建立work目录,并在其中建立同名文本文档work.txt,在txt文件中写入当前日期的月和日便可
    def chuangjian(self):

        #判断有没有对应文件夹
        if os.path.exists(self.workpath):
            #若是有,就删除该文件
            shutil.rmtree(self.workpath)
            print('文件已删除')
        else:
            # 建立文件夹
            os.mkdir('work')
            #进入文件夹
            os.chdir('work')
            f=open('work.txt','w')
            #建立时间
            s = str(datetime.datetime.now())
            #将时间写入文件2020-07-27 10:51:09.800656
            f.write(s[5:7]+s[8:10])

        #设置时间
        print(s[5:7]+s[8:10])


    #2. 链接虚拟机,将刚才写好的work.txt上传到虚拟机/home/目录下,同时将本地的work.txt删除
    def shangchuan(self):
        #获取通道
        ssh=paramiko.Transport(self.ip,self.port)
        ssh.connect(username=self.user,password=self.password)
        p=paramiko.SFTPClient.from_transport(ssh)
        #判断有没有该文件
        if os.path.exists(self.workpath):
            #读出目录的文件
            file1= os.listdir(self.workpath)
            if len(file1) == 0:
                print('目录为空..')
            else:
                #拼接路径
                newfile=''.join(file1)
                #拼接路径    本地路径                                linux路径
                p.put(os.path.join(self.workpath,newfile),f'{self.linuxpath}/{newfile}')
                print('文件已上传.....')
                os.remove(os.path.join(self.workpath,newfile))

                if len(os.listdir(self.workpath)) == 0:
                    print('本地文件已删除')
                else:
                    print('删除失败!!!')
        else:
            print("本地文件路径错误。。。。。")

    # 3.
    # Linuxetc目录下的passwd, shadow, group三个文件使用tar命令进行打包,包名为work.txt    # 的内容加上文件的首字母,例如:0725
    # p.tar.gz0725
    # s.tar.gz0725
    # g.tar.gz,将压缩后的三个
    # 文件下载到本地work目录
    def xiazai(self):
        #操做linux死格式
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=self.ip,port=self.port,username=self.user,password=self.password)
#获取work文件中的月日信息
        #读出linux中的work.txt文件
        stdin, stdout, stderr = ssh.exec_command(f'cat {self.linuxpath}/work.txt')
        time=stdout.read().decode('utf-8')
        print(time)

#压缩文件----获取压缩后的文件名字
        #设置个集合存储要压缩的文件名
        list1=['passwd','shadow','group']
        #压缩到home        for i in list1:
            ssh.exec_command(f'tar -zcvf {self.linuxpath}/{time}{i[0]}.tar.gz /etc/{i}')
        #输出home下的内容
        stdin, stdout, stderr = ssh.exec_command(f'cd {self.linuxpath}/;ls')
        #读出数据
        allstr = stdout.read().decode('utf-8').replace('\n', '')
        #print(allstr)#0727g.tar.gz0727p.tar.gz0727s.tar.gzlrk1work.txt
        #将数据分割
        tarstr = allstr[:12] + ',' + allstr[12:24] + ',' + allstr[24:36]
        tarlist = tarstr.split(',')
        print(tarlist)

#下载
        #循环名字
        for i in tarlist:#['0727g.tar.gz', '0727p.tar.gz', '0727s.tar.gz']
            #设置通道
            s = paramiko.Transport(self.ip, self.port)
            s.connect(username=self.user, password=self.password)
            p = paramiko.SFTPClient.from_transport(s)

            if os.path.exists(self.workpath):
                #下载文件
                p.get(self.linuxpath+'/'+i,os.path.join(self.workpath,i))
                print(f'{i}压缩包已下载')
            else:
                print('work路径不存在,没法下载到本地')

#调用类方法
li=linux('192.168.56.1', 22, 'root', '123456', r'D:\pythoncode\work', '/home')
# li.chuangjian()第一问,建立文件,写入内容
# li.shangchuan()#第二问,上传
li.xiazai()#第三问,压缩文件,下载到本地
相关文章
相关标签/搜索