使用stdin代替提示符输入zip命令

3

我希望能够用密码保护我的ZIP压缩文件。

请按照以下步骤创建压缩文件:

zip -er archivename.zip file

并且获取此内容:

Enter password: 
Verify password: 

一切都没问题,但我有一个带密码的变量,需要将此密码发送给 zip。尝试使用类似这样的参数,但失败了。

echo "$1"| zip -er archivename.zip file

在bash中是否可能?因为zip实用程序不支持stdin。
2个回答

6

阅读手册

如果您需要不安全的解决方案,请使用-P

-P|--password password 将密码用于加密 压缩文件条目(如果有)。这是不安全的!

用户交互

-e|--encrypt 使用在终端上输入的密码来加密 zip归档的内容,以响应提示


5

这个有一个参数,可以查看manpage:

-P password
--password password
       Use password to encrypt zipfile entries (if any).  THIS IS INSECURE!  Many multi-user operating systems  provide  ways
       for any user to see the current command line of any other user; even on stand-alone systems there is always the threat
       of over-the-shoulder peeking.  Storing the plaintext password as part of a command line in an automated script is even
       worse.   Whenever  possible, use the non-echoing, interactive prompt to enter passwords.  (And where security is truly
       important, use strong encryption such as Pretty Good Privacy instead of the relatively weak standard  encryption  pro‐
       vided by zipfile utilities.)

编辑:如果这太不安全了,请使用这个简单的expect脚本:

#!/usr/bin/expect -f

set prompt {$ }
set password "secret"

#spawn the command
spawn zip -er archivename.zip file

# Look for passwod prompt (twice)
expect "*?assword:*"
send "$password\r"

expect "*?assword:*"
send "$password\r"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接