简体   繁体   English

Perl脚本使用gpg还是CLI gpg?

[英]Perl script use of gpg vs. CLI gpg?

As part of a perl script, I am downloading some files from a website based on cycle numbers obtained from emails. 作为perl脚本的一部分,我正在根据从电子邮件获得的周期号从网站上下载一些文件。 I use a regexp to find the appropriate cycle number, append that to a url and grab an encrypted file from said url. 我使用正则表达式找到合适的循环号,将其附加到URL并从所述URL中获取加密的文件。 I then print the encrypted file to a temp file, and use gpg to decrypt that temp file, dumping the output into another location. 然后,我将加密的文件打印到一个临时文件,并使用gpg解密该临时文件,将输出转储到另一个位置。 Clear as mud? 像泥一样清澈?

Here's where things get interesting. 这是有趣的地方。 This is the command that is used to decrypt the temp file: 这是用于解密临时文件的命令:

my $cmd = 'cat ~/.gpgkey | gpg --passphrase-fd 0 --no-tty --batch --quiet 
          --no-mdc-warning --decrypt $filename >> ~/SAT.SCR';

If you'll notice, the command uses a $filename variable. 如果您会注意到,该命令使用$ filename变量。 I have separately confirmed that this variable is pointing to the correct file. 我已经单独确认此变量指向正确的文件。 However, this command yields the following error: 但是,此命令产生以下错误:

gpg: decrypt_message failed: Unknown system error

If, however, I run the above command straight from the shell (csh), replacing $filename with the actual file, it works perfectly. 但是,如果我直接从外壳程序(csh)运行上述命令,将$ filename替换为实际文件,则可以正常运行。 We are using gpg 2.0.9. 我们正在使用gpg 2.0.9。

Does anyone have an idea why the decryption is failing when called from the perl script but not from the shell? 有谁知道为什么从perl脚本而不是从shell调用时解密失败?

Perl doesn't interpolate variables in strings with single quotes. Perl不会用单引号插入字符串中的变量。 Try using a string with double quotes: 尝试使用带双引号的字符串:

my $cmd = "cat ~/.gpgkey | gpg --passphrase-fd 0 --no-tty --batch --quiet 
      --no-mdc-warning --decrypt $filename >> ~/SAT.SCR";
system($cmd);

See Quote and Quote-like Operators in perlop for more information. 有关更多信息,请参见perlop中的Quote和类似Quote的运算符

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

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