这个问题是后续问题这个问题,因为我发现我在上一个问题中提出的解决方案不再有效。它有时有效,但现在expect
不能按预期工作。这是完整的代码:
#!/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
'
此 bash 脚本不会加密文件test.upd
,而是在我的目录中创建以“zi”开头的文件,例如zi0gA1rU
或zi8Vv6OD
。
这是为什么?那些文件是什么?为什么上面的脚本有时有效,有时无效?难道就没有办法让expect
自己的行为举止得体吗?
我在哪里可以找到 的源代码zipcloak
,以便我可以重写它以避免使用expect
?
附加信息:
我必须以 执行此命令
root
,因为 zip 文件具有 root 权限。也许这很重要?zipcloak -v 版权所有 (c) 1990-2008 Info-ZIP - 输入“zipcloak "-L"”以获得软件许可证。
这是 ZipCloak 3.0(2008 年 7 月 5 日),由 Info-ZIP 开发。目前由 E. Gordon 维护。请使用 www.info-zip.org 网页向作者发送错误报告;有关详细信息,请参阅 README。
最新源代码和可执行文件位于ftp://ftp.info-zip.org/pub/infozip,截至上述日期;看http://www.info-zip.org/对于其他网站。
2011 年 6 月 11 日使用 gcc 4.6.1 for Unix (Linux ELF) 编译。
ZipCloak 特殊编译选项:[加密,2007 年 1 月 5 日版本 2.91]
当它停止工作后,我需要将最后一个设置sleep
为2。然后它又工作了。但后来,这也行不通了。所以我把时间设置sleep
为5秒。目前,我的脚本正在运行。但不可靠。
使用 set 调试输出exp_internal 1
:
spawn /usr/bin/test.upd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {28746}
expect: does "" (spawn_id exp6) match glob pattern "*Enter password*"? no
Enter password:
expect: does "Enter password: " (spawn_id exp6) match glob pattern "*Enter password*"? yes
expect: set expect_out(0,string) "Enter password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Enter password: "
send: sending "XXX\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "*Verify password*"? no
Verify password:
expect: does "\r\nVerify password: " (spawn_id exp6) match glob pattern "*Verify password*"? yes
expect: set expect_out(0,string) "\r\nVerify password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\nVerify password: "
send: sending "XXX\r" to { exp6 }
argv[0] = expect argv[1] = -c argv[2] =
exp_internal 1
spawn /usr/bin/zipcloak "test.upd"
expect "*Enter password*"
sleep 1
send "XXX\r"
sleep 1
expect "*Verify password*"
sleep 1
send "XXX\r"
sleep 1
set argc 0
set argv0 "expect"
set argv ""
更新:Glenn 发布的第二个脚本的输出是:
spawn zipcloak test.upd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {9956}
Gate keeper glob pattern for 'password: $' is 'password: '. Activating booster.
expect: does "" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
Enter password:
expect: does "Enter password: " (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=yes re=yes
expect: set expect_out(0,string) "password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Enter password: "
send: sending "mypassword\r" to { exp6 }
expect: continuing expect
expect: does "" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
mypassword
expect: does "mypassword\r\n" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
expect: timed out
argv[0] = expect argv[1] = -c argv[2] =
exp_internal 1
spawn zipcloak test.upd
expect {
-re {password: $} {send "mypassword\r"; exp_continue}
eof
}
set argc 0
set argv0 "expect"
set argv ""
更新
我实际上想在命令行上指定文件名和密码来调用代码
./myscript.sh test.upd pass
这是脚本:
#!/bin/bash
expect -c "
exp_internal 1
spawn zipcloak \"$1\"
expect {
-re {password: \$} {send \"$2\r\"; exp_continue}
expect eof
}
"
但是,它会再次创建一个以 开头的文件zi
,并且不会加密实际文件。我不知道每一个看似逻辑上的小变化都会破坏expect脚本的整个工作流程。这是输出:
spawn zipcloak test.upd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {16791}
Gate keeper glob pattern for 'password: $' is 'password: '. Activating booster.
expect: does "" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
"expect"? no
Enter password:
expect: does "Enter password: " (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=yes re=yes
expect: set expect_out(0,string) "password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Enter password: "
send: sending "pass\r" to { exp6 }
expect: continuing expect
expect: does "" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
"expect"? no
pass
expect: does "pass\r\n" (spawn_id exp6) match regular expression "password: $"? Gate "password: "? gate=no
"expect"? no
expect: timed out
argv[0] = expect argv[1] = -c argv[2] =
exp_internal 1
spawn zipcloak "test.upd"
expect {
-re {password: $} {send "pass\r"; exp_continue}
expect eof
}
set argc 0
set argv0 "expect"
set argv ""
答案1
我会把 zipcloak-er 写成
#!/usr/bin/env expect
set zipfile [lindex $argv 0]
if {![file exists $zipfile]} {error "no such zipfile: $zipfile"}
set pass "mypassword"
spawn zipcloak $zipfile
expect {
-re {password: $} {send "$pass\r"; exp_continue}
eof
}
exp_continue
由于“输入密码:”和“验证密码:”提示具有共同的后缀,因此重用代码是有意义的。
“不起作用”没有帮助。你用了吗exp_internal 1
?尝试这个:
#!/bin/bash
MYPWD="mypassword"
expect -c "
exp_internal 1
spawn zipcloak test.upd
expect {
-re {password: \$} {send \"$MYPWD\\r\"; exp_continue}
eof
}
"
抱歉亚历克斯,我不明白你的问题。这就是我在 bash 中发生的情况:
$ zip test.upd file
adding: file (deflated 22%)
$ MYPWD="mypassword"
$ expect -c "
exp_internal 1
spawn zipcloak test.upd
expect {
-re {password: \$} {send \"$MYPWD\\r\"; exp_continue}
eof
}
"
spawn zipcloak test.upd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {27485}
Gate keeper glob pattern for 'password: $' is 'password: '. Activating booster.
expect: does "" (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=no
Enter password:
expect: does "Enter password: " (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=yes re=yes
expect: set expect_out(0,string) "password: "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Enter password: "
send: sending "mypassword\r" to { exp4 }
expect: continuing expect
expect: does "" (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=no
Verify password:
expect: does "\r\nVerify password: " (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=yes re=yes
expect: set expect_out(0,string) "password: "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\nVerify password: "
send: sending "mypassword\r" to { exp4 }
expect: continuing expect
expect: does "" (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=no
expect: does "\r\n" (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=no
encrypting: file
expect: does "\r\nencrypting: file\r\n" (spawn_id exp4) match regular expression "password: $"? Gate "password: "? gate=no
expect: read eof
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\nencrypting: file\r\n"
argv[0] = expect argv[1] = -c argv[2] =
exp_internal 1
spawn zipcloak test.upd
expect {
-re {password: $} {send "mypassword\r"; exp_continue}
eof
}
set argc 0
set argv0 "expect"
set argv ""