7

我想做类似的事情

openssl enc -d -aes256 -in somefile | less

openssl需要来自 的密码stdinless当涉及到这一切时,这一切都搞砸了。

有没有办法从交互式命令中获取输出(例如openssl要求输入密码)并将输出通过管道传输到less

还是使用 bash 脚本有更好的技术?

4

2 回答 2

3

也许让 shell 脚本请求密钥,然后将密钥存储在临时文件中并使用 openssl 的 -kfile 选项来找到它。希望您的 openssl 版本支持 -kfile。

我会担心这个的安全性,但稍加注意,安全漏洞可能比你想象的要小。(但是你相信你的系统管理员和 sudoers...?)

#!/bin/bash

INFILE=somefile

read -s -p "Enter key for $INFILE: " key
echo

# write the key to a temp file; use mktemp(1)
# for safer creation of a privately-owned file.
#
TMPKEY=$(mktemp -t) || exit 1
echo "$key" > "$TMPKEY"

# will remove the temp file on script exit
trap 'rm -f "$TMPKEY"' EXIT

# remove the key file a couple seconds after openssl runs
(sleep 2; rm -f "$TMPKEY") &

openssl enc -d -aes256 -in "$INFILE" -kfile "$TMPKEY" | less

[ -f "$TMPKEY" ] && rm -f "$TMPKEY"
trap - EXIT

# rest of script...

exit 0
于 2013-08-23T03:23:22.860 回答
1

你可以试试这个:

echo 'mypassword' | openssl enc -d -aes256 -in somefile | less

但这看起来并不安全。

我没有尝试过以openssl这种方式运行,但是如果它太冗长并且如果以前的代码不起作用,那么您可以随时尝试使用expect。这是一个例子:

expect -c '
    spawn yourscript
    expect "Please enter your password:"
    send "$PASSWORD"
'
于 2013-08-23T02:38:16.257 回答