我想做类似于以下的事情
openssl enc -d -aes256 -in somefile | less
openssl
需要从stdin
获取密码。当使用less
时,这会变得非常混乱。
有没有一种方法可以将交互式命令(例如要求密码的openssl
)的输出导入到less
中?
或者是否有一种更好的技术可以使用bash脚本来完成?
#!/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
myFile='my awesome file'
rm "$myFile"
将删除一个名为“my awesome file”的文件,这是正确的。但是,rm $myFile
将删除三个文件:my
、awesome
和file
,这是错误的和不可预测的,除非你确实知道自己在做什么。 - Aleks-Daniel Jakimenko-A.
echo 'mypassword' | openssl enc -d -aes256 -in somefile | less
但这看起来不太安全。
我还没有尝试过使用此方式运行openssl
,但如果它过于冗长,或者之前的代码无法工作,您始终可以尝试使用 expect。以下是一个例子:
expect -c '
spawn yourscript
expect "Please enter your password:"
send "$PASSWORD"
'
$tmp_file=$(mktemp); openssl enc -d -in somefile > $tmp_file; less $tmp_file; rm $tmp_file
- patrick hrsync
通过ssh
进行此操作,通常没有问题。当出现混乱时,似乎是一个时间问题 - 已有一段时间没有发生了,但我记得如果rsync在请求密码时慢了,情况就会变得很糟糕。 - drevicko