linux: 加密文件夹的一种python自动化方式

我在mac 上实现,你需要

  • pexpect
  • python 2.7

ref1

https://www.cnblogs.com/kevingrace/p/8194784.html

note

方法五: ZIP加密

方法三:结合Tar和OpenSSL给文件和目录加密及解密

$ mkdir openssl_tar_test
$ cd openssl_tar_test
$ echo "abc" > a.md
$ echo "xyz" > b.md
$ ls
a.md b.md
## 这就是如何加密的主要操作
$ tar -czf - * | openssl enc -e -aes256 -out test.tar.gz
enter aes-256-cbc encryption password:##你在这里输入了密码
Verifying - enter aes-256-cbc encryption password:# 并在这里verify
$ ls
a.md        b.md        test.tar.gz# 现在你看到了新的加密文件
$ tar -zxvf test.tar.gz  # 现在你看,不可以随便解压
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
## 这就是如何解密的主要操作
$ openssl enc -d -aes256 -in test.tar.gz| tar xz -C /Users/paul/code/c_test
enter aes-256-cbc decryption password:
$ # 去c_test文件夹,你可以看到解密的2个文件

写一个自动的脚本

1. 你要参考如何让py执行linux 的命令行

你使用subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,shell=shell,bufsize=4096)
#!/usr/bin/python
# coding=utf-8
'''
Created on 2013年11月22日
  
@author: crazyant.net
'''
import shlex
import datetime
import subprocess
import time
  
def execute_command(cmdstring, cwd=None, timeout=None, shell=False):
  """执行一个SHELL命令
      封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr
      参数:
    cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd
    timeout: 超时时间,秒,支持小数,精度0.1秒
    shell: 是否通过shell运行
  Returns: return_code
  Raises: Exception: 执行超时
  """
  if shell:
    cmdstring_list = cmdstring
  else:
    cmdstring_list = shlex.split(cmdstring)
  if timeout:
    end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
   
  #没有指定标准输出和错误输出的管道,因此会打印到屏幕上;
  sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,shell=shell,bufsize=4096)
   
  #subprocess.poll()方法:检查子进程是否结束了,如果结束了,设定并返回码,放在subprocess.returncode变量中 
  while sub.poll() is None:
    time.sleep(0.1)
    if timeout:
      if end_time <= datetime.datetime.now():
        raise Exception("Timeout:%s"%cmdstring)
       
  return str(sub.returncode)
  
if __name__=="__main__":
  print execute_command("ls")

2. 你还要处理与命令行的交互,这是subprocess难以做到的

3. 于是, 你使用pexpect

python, linux shell 使用expect模块自动输入密码

https://blog.csdn.net/xj626852095/article/details/51271541

Pexpect 是一个自动控制的 Python 模块,可以用来ssh、ftp、passwd、telnet 等命令行进行自动交互。
官方网站是 http://www.noah.org/
通过它,可以实现类似 expect 的操作。
例如我们可以用它来写python脚本,实现批量对一系列(大量的、配置相同的)的linux服务器进行操作。

如果你有时间,可以研究一下源码:

https://github.com/pexpect/pexpect

3.1 然后,我如何实现自动化加密软件
#!/usr/bin/python


import pexpect

if __name__ == '__main__':
	mypass = "123456"
	shell_cmd = 'tar -czf - * | openssl enc -e -aes256 -out encrypted.tar.gz'
	child = pexpect.spawn('/bin/bash', ['-c',shell_cmd])
	child.expect('(?i)password')
	child.sendline(mypass)
	child.expect('(?i)password')
	child.sendline(mypass)
	#print child.before   # Print the result of the ls command.  
	child.interact()
	pass
3.2 实现解密
#!/usr/bin/python

import pexpect

if __name__ == '__main__':
	mypass = "123456"
	shell_cmd = 'openssl enc -d -aes256 -in encrypted.tar.gz| tar xz -C .'
	child = pexpect.spawn('/bin/bash', ['-c',shell_cmd])
	child.expect('(?i)password')
	child.sendline(mypass)
	#print child.before   # Print the result of the ls command.  
	child.interact()
	pass

4 还需要读入命令行用户输入的密码

part3 的密码 : 123456 是写死在代码里的,这不好。

4.1 给出你dencry的读入命令行参数的代码,其他类似
#!/usr/bin/python

import pexpect
import sys #this is u need 

if __name__ == '__main__':
	mypass = sys.argv[1] # and here is also u need to add, that is all.
	shell_cmd = 'openssl enc -d -aes256 -in encrypted.tar.gz| tar xz -C .'
	child = pexpect.spawn('/bin/bash', ['-c',shell_cmd])
	child.expect('(?i)password')
	child.sendline(mypass)
	#print child.before   # Print the result of the ls command.  
	child.interact()
	pass
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值