简体   繁体   English

带有gpg的Python pexpect

[英]Python pexpect with gpg

I'm trying to get Python pexpect to work with GNU gpg. 我正在尝试让Python能够与GNU gpg一起使用。 We do not have the gnupg module loaded and trying to do so would be nearly impossible. 我们没有加载gnupg模块,尝试这样做几乎是不可能的。

I can get pexepct to work doing FTP and SSH, but with gpg I'm having a slight problem. 我可以让pexepct来执行FTP和SSH,但是使用gpg时我有一个小问题。 Below is the command I run to decrypt the file from the command line. 下面是我运行以从命令行解密文件的命令。 I enter the passphrase at the prompt and it works perfectly. 我在提示符下输入密码,它可以正常工作。

# gpg --decrypt encKey_20141009_b7489540_36.00.tar.gpg > encKey_20141009_b7489540_36.00.tar

You need a passphrase to unlock the secret key for user: "XXXXXXXXXXXXXXXXXXXXXXXXXXX" 2048-bit RSA key, ID AD22F55F, created 2013-07-18 (main key ID 0D56620B) 您需要一个密码来解锁用户的密钥:“ XXXXXXXXXXXXXXXXXXXXXXXXXXX” 204​​8位RSA密钥,ID AD22F55F,创建于2013-07-18(主密钥ID 0D56620B)

Enter passphrase: 输入密码:

Yet when I try to use pexpect I have a problem. 但是,当我尝试使用pexpect时,我遇到了问题。 Below is the code that I'm trying to get to work: 以下是我要开始工作的代码:

#!/usr/bin/python

import pexpect
Password = raw_input('Enter passphrase:  ')

child = pexpect.spawn('gpg --decrypt encKey_20141009.tar.gpg > encKey_20141009_.tar')
child.expect('Enter passphrase: ')
child.sendline(Password)

Below is the error that I receive: 以下是我收到的错误:

Traceback (most recent call last):
  File "./pexepect.py", line 9, in ?
child.expect('Enter passphrase: ')
 File "/usr/lib/python2.4/site-packages/pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1325, in expect_list
  return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1396, in expect_loop
raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pexpect.spawn object at 0x2b2dfe13f350>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/gpg
args: ['/usr/bin/gpg', '--decrypt', 'encKey_20141009.tar.gpg', '>', 'encKey_20141009.tar']
searcher: searcher_re:
    0: re.compile("Enter passphrase: ")
buffer (last 100 chars):
before (last 100 chars): usage: gpg [options] --decrypt [filename]

after: pexpect.EOF
match: None
match_index: None
exitstatus: 2
flag_eof: True
pid: 14399
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

> is a redirection in shell. >是Shell中的重定向。 pexpect doesn't run shell, it runs gpg executable. pexpect不运行shell,而是运行gpg可执行文件。 Specify the output file using gpg options instead ( --output ). 改用gpg选项指定输出文件( --output )。

Here's how to run gpg using subprocess module: 以下是使用subprocess模块运行gpg方法:

#!/usr/bin/env python3
import os
from subprocess import Popen

passphrase = input('Enter passphrase: ')
file_to_decrypt = 'encKey_20141009.tar.gpg'
in_fd, out_fd = os.pipe()
cmd = ['gpg', '--passphrase-fd', str(in_fd)]
cmd += ['--output', os.path.splitext(file_to_decrypt)[0]]
cmd += ['--decrypt', file_to_decrypt]
with Popen(cmd, pass_fds=[in_fd]):
    os.close(in_fd) # unused in the parent
    with open(out_fd, 'w') as out_file:
        out_file.write(passphrase)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM