当shell提示时,脚本能提供输入吗



假设我想制作一堆充满胡言乱语的文件。如果我想要一个胡言乱语的文件,然后使用ccrypt加密它,我可以这样做:

CCD_ 1,现在使用ccrypt:

$ ccrypt -e randomfile.txt
Enter encryption key: 
Enter encryption key: (repeat)

如您所见,系统会提示我输入密钥。

我想把它自动化,并创建一堆胡言乱语的文件。

python中生成随机胡言乱语的脚本:

import random as rd
import string as st
alphs = st.ascii_letters
digits = st.digits
word = ""
while len(word) < 1000:
word += str(rd.choices(alphs))
word += str(rd.choices(digits))
print(word)

现在从bash脚本运行这个,将胡言乱语保存到file:

#!/bin/bash
count=1
while [ $count -le 100 ]
do
python3 /path/r.py > "file$count.txt"
ccrypt -e "file$count.txt"
((count=count+1))
done

问题,正如你所看到的:

$ bash random.sh 
Enter encryption key:

ccrypt没有提供密码短语作为参数的选项。

问题:bash脚本有没有办法在shell提示时提供密码短语?

我知道只需在python中进行加密就可以解决这个问题,但我很好奇bash是否可以完成这样的操作。

如果重要的话:ccrypt可以选择只要求一个提示。

[编辑]

我最初建议的答案是:

printf "$PASSPHRASEn$PASSPHRASEn" | ccrypt -e "file$count.txt"

这是一个通用的解决方案,应该与许多期望将一些输入传递给STDIN的工具一起使用;但无论出于何种原因,它似乎都不适用于CCD_ 5。

然而,ccrypt也有以不同(非交互式(方式提供密码短语的选项:

$ ccrypt --help
...
-K, --key key         give keyword on command line (unsafe)
-k, --keyfile file    read keyword(s) as first line(s) from file
...

下面是一个使用-K的示例。注意,它是";"不安全";因为如果您在交互式shell中执行此命令,或者使用-x运行脚本(打印每个执行的命令(,密码短语可能会分别出现在~/.bash_history或某些日志中,因此请将密码短语转储到文件中,并在重要情况下使用$ echo "12 ddsd23" > randomfile.txt0。

#!/bin/bash
# read the passphrase, do not display it to screen
read -p "Please provide a passphrase:" -s PASSPHRASE
count=1
while [ $count -le 100 ]
do
python script.py > "file$count.txt"
ccrypt -e "file$count.txt" -K "$PASSPHRASE"
((count=count+1))
done

您需要在bash代码中使用yes命令。基本上,这个命令会在需要的时候为脚本(即ccrypt(提供输入。查看此处了解更多信息。

最新更新