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.
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
    '
  • Do you need the two sleeps after the sends? – ctrl-alt-delor Nov 26 '13 at 9:27
  • Yes, I need to two sleeps after the two send. Otherwise the zipfile is not encrypted. – Alex Nov 26 '13 at 9:51
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.

  • 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 '13 at 6:24
  • Did you try to run zipcloak manually? – ctrl-alt-delor Nov 26 '13 at 9:25
  • Yes, of cource I used zipcloak manually; also to see how to use it. – Alex Nov 26 '13 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.