2

I create a zip file using zip which includes several files and directories, named test.upd. Now I want to use expect to automatically handle the password encryption. Here are two bash scripts, both do not work:

!/bin/bash

MYPWD="mypassword"

expect -c '
    spawn zipcloak test.upd
    expect {
       "*Enter password*" { send "'"$MYPWD"'\r"
                                   exp_continue
                                  }
       "*Verify password*" { send "'"$MYPWD"'\r"
                                      exp_continue
                                    }
    }'

and this file:

!/bin/bash
MYPWD="mypassword"

expect -c '
    spawn zipcloak test.upd
    expect "*Enter password*" 
    send "'"$MYPWD"'\r"
    expect "*Verify password*" 
    send "'"$MYPWD"'\r"

    }'

I also tried to create an expect script directly:

#!/usr/bin/expect -f

spawn zipcloak test.upd
expect "*Enter password*" 
send "password\r"
expect "*Verify password*" 
send "password\r"

All scripts seem to send correctly the first password request, but it looks like the text for the second password entry is never asked. How can I do it right?

Constraints:

  • I have to use zipcloak (NOT zip)
  • I want to use expect to automatically set the password.

Remarks:

  • I have used the correct script
  • Each time, when using the first script, an empty file starting with zi is created in the directory, like ziSvbYhy or zi3gzEQ4.
0

3 Answers 3

1

The solution seem to be to wait some time before actions! I have absolutely no idea why this is (comments, explanations and insights welcome), but the following script works now for me:

#!/bin/bash
MYPWD="mypassword"

expect -c ' 
    spawn zipcloak test.upd
    expect "*Enter password*" 
    sleep 1
    send  "'"$MYPWD"'\r"
    sleep 1
    expect "*Verify password*" 
    sleep 1
    send  "'"$MYPWD"'\r"
    sleep 1
    '
2
  • Do you need the two sleeps after the sends? Nov 26, 2013 at 9:27
  • Yes, I need to two sleeps after the two send. Otherwise the zipfile is not encrypted.
    – Alex
    Nov 26, 2013 at 9:51
1

It is my interpretation that the answers from Alex and ctrl-alt-delor don't properly wait for the zipcloak process to finish and therefore are prone to leave the zip file unencrypted (and leave a temporary file with a name such as zimfwtpJ behind). So their code work for small zip files but fail for larger ones.

Here is my solution:

#!/bin/bash
MYPWD="mypassword"

expect -c '
    set timeout -1
    spawn zipcloak test.upd
    expect "*Enter password*"
    send  "'"$MYPWD"'\r"
    expect "*Verify password*"
    send  "'"$MYPWD"'\r"
    expect eof
    '
0

The first example in the question worked for me.

!/bin/bash
MYPWD="mypassword"

expect -c '
     spawn zipcloak test.upd
     expect {
        "*Enter password*" { send "'"$MYPWD"'\r"
                               exp_continue
                              }
        "*Verify password*" { send "'"$MYPWD"'\r"
                                  exp_continue
                                }
}'

Though the second did not. Try it again, maybe you where running the wrong script. That often happens to me: I wonder why it does not work, but I am running the wrong script.

3
  • Nope, this script does NOT work for me. The zipped files is NOT encrypted after the procedure. Maybe there is a difference in the version of zipcloak or expect, or some libraries are differrent? I have double- and triple checked to run the script EXACTLY as it is! Also, each time a file like ziSvbYhy is created in the directory (different names).
    – Alex
    Nov 26, 2013 at 6:24
  • Did you try to run zipcloak manually? Nov 26, 2013 at 9:25
  • Yes, of cource I used zipcloak manually; also to see how to use it.
    – Alex
    Nov 26, 2013 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .