2017-10-04 47 views
1

我想用子進程通過python腳本運行一個openssl命令來解密已知密碼的已加密文件。Python:通過子進程使用openssl來解密一個可變密碼

代碼我有工作,但我希望能夠保存一個密碼作爲變量,即pass = 'password',並將該變量輸入到子進程調用中,而不是靜態地將其作爲pass:password。不用擔心我如何將密碼保存到變量中,只關注將它作爲變量傳遞給子進程調用,這可能嗎?

我正在閱讀OpenSSL手冊頁的PASS PHRASE ARGUEMENTS部分,所以我很好奇我是否應該使用其他選項之一(env:var, file:pathname, fd:number, or stdin)。但是很難找到它們的使用示例,所以我不確定。

這是我到目前爲止有:

def decryptfile(): 
decryptFile = subprocess.Popen(["openssl", "aes-256-cbc", "-d", "-a", "-in", "pw.txt", 
           "-out", "pw.dec.txt", "-pass", "pass:password"] 
) 

回答

0

我與蟒蛇工作了相當長的一段時間了,我知道如何從子模塊的輸出存儲到一個用戶定義的變量如下

import subprocess 

value = subprocess.getoutput("Your Command") 
print("The variable value is holding: {}".format(value)) 

只要將密碼作爲變量嘗試將子流程輸出(現在存儲在變量中)的數據拆分爲列表。您也可以將該列表格式化爲所需內容。您可以設置密碼變量與價值相當於到該列表內的值通過索引,列出的值,例如:

hello = subprocess.getoutput("echo hello") 
list_hello = list(hello) 
# print the index value of 1 to return hello 
# use string interpolation 
print("Second value in subprocess call is : {}".format(list_hello[1])) 
# store the value of "hello" in "echo hello" 
# to a variable 
hello_str = str(list_hello[1]) 
# you should cast the variable to a string 
# data type just to be safe 
print("The value of hello_str is: {}".format(hello_str)) 
+0

這不完全是我想要的,我會盡力澄清它。我需要能夠將一個已經定義好的變量輸入到子進程調用中,而不是使用'pass:password'它使用我的變量,並且不會等待我輸入密碼到終端中。關於存儲輸出的東西肯定會在我的程序中稍後有用,儘管 – Brosta

0

使用字符串插值

value = "my_value" 
subprocess.call("commands {}".format(value), shell=True) 

使用該字符串插值方法調用/包括你的變量與你的SSL命令(密碼)

+0

希望有所幫助,或者至少將您指向正確的方向 – user8628164

+0

謝謝我會盡力在現在工作,並讓你知道它是怎麼回事! – Brosta