Alexey
Alexey

Reputation: 221

stdin to zip command instead of prompt

Want to protect my ZIP archive with password.

Create archive with this:

zip -er archivename.zip file

and get this:

Enter password: 
Verify password: 

Everything is ok,but I have variable with password and need to send this password to zip. Tried through arguments like this, but failed.

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

Is it possible in bash? Because zip utility does not support stdin.

Upvotes: 3

Views: 4291

Answers (2)

chaos
chaos

Reputation: 9282

There is a parameter for this see the 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.)

EDIT: If this is to unsecure for you use this simple expect script:

#!/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"

Upvotes: 5

Édouard Lopez
Édouard Lopez

Reputation: 43391

Read the man

For insecure solution, use -P:

-P|--password password Use password to encrypt zipfile entries (if any). THIS IS INSECURE!

User interaction

-e|--encrypt Encrypt the contents of the zip archive using a password which is entered on the terminal in response to a prompt

Upvotes: 6

Related Questions