在python脚本中使用带有openssl的子流程

在python脚本中使用带有openssl的子流程,python,openssl,subprocess,Python,Openssl,Subprocess,我完全搞不懂Python子进程语法 我想在Python脚本中使用openssl解密字符串 下面是可用的bash脚本片段: readable_code=$(echo "$encrypted_code"| openssl enc -aes-128-cbc -a -d -salt -pass pass:$key) 因此,在python脚本中,我知道要运行相同的bash命令,我应该使用subprocess。 我需要通过管道将echo传递到openssl命令,并动态地传递加密的_代码和密钥变量(在循环中

我完全搞不懂Python子进程语法

我想在Python脚本中使用openssl解密字符串

下面是可用的bash脚本片段:

readable_code=$(echo "$encrypted_code"| openssl enc -aes-128-cbc -a -d -salt -pass pass:$key)
因此,在python脚本中,我知道要运行相同的bash命令,我应该使用subprocess。 我需要通过管道将echo传递到openssl命令,并动态地传递加密的_代码和密钥变量(在循环中)

有人知道这个的正确语法吗? 下面的代码片段应该是我尝试做什么的背景。 谢谢

import subprocess 

    key = "my-secret-key"
    file = list_of_ips #format ip:long-encrypted-code

with open(file_read) as f:
    #read in all connecion requests
    content=f.readlines()
    #create  list that will hold all ips whose decrypted codes have passed test
    elements = []
    for ip_code in content:
        #grab the ip address before the colon
        ip = ip_code.split(':', 1)[0]
        #grab the encrypted code after the colon
        code = ip_code.split(':',1)[1]

        #here is where I want to run the bash command and assign to a python variable
        decrypted_code = subprocess....using code and key variables
        ...on it goes....

我强烈建议您使用Python库来编写shell脚本。 特别是它有一个和

我不太明白您试图解决的确切任务是什么,但您的代码可能大致如下所示:

from plubum.cmd import openssl

with open('file') as f:
    for ip_code in f:
        (openssl['whatever', 'params'] << ip_code)()
从plubum.cmd导入openssl
以“打开”(“文件”)作为f:
对于f中的ip_代码:

(openssl['whatever','params']来模拟shell命令:

$ readable_code=$(echo "$encrypted_code"| openssl enc -aes-128-cbc -a -d -salt -pass "pass:$key")
在Python中使用
子流程
模块:

from subprocess import Popen, PIPE

cmd = 'openssl enc -aes-128-cbc -a -d -salt -pass'.split()
p = Popen(cmd + ['pass:' + key], stdin=PIPE, stdout=PIPE)
readable_code = p.communicate(encrypted_code)[0]

嗨,吉尔-它工作了!感谢您的快速响应。这是我的代码:解密的_代码=(openssl['enc','-aes-128-cbc','-a','-d','-salt','-pass',pass:'+key]