我想要自动化的与 cryptsetup 的简单交互是这样的:
cryptsetup luksFormat /dev/loop20
WARNING: Device /dev/loop20 already contains a 'crypto_LUKS' superblock signature.
WARNING!
========
This will overwrite data on /dev/loop20 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase for /dev/loop20:
Verify passphrase:
以下期望脚本尝试执行此操作:
spawn cryptsetup luksFormat /dev/loop20
expect "yes): " { send "YES\n" }
expect ": "
send "passphrase\n"
expect ": "
send "passphrase\n"
expect eof
虽然 YES 被正确发送,但它会在密码提示处挂起。我也尝试使用 此外 在发送命令中。
相同的脚本结构适用于 ssh 密码或 sudo 密码提示,所以我有点迷失这里的不同之处。我知道有一些“无预期”的方法,例如 echo passphrase | cryptsetup ...,但我真的很想了解根本问题以及是否有解决方案。
meuh 的评论是解决方案:密码提示后我需要稍等一下。
所以这有效:
spawn cryptsetup luksFormat /dev/loop20
expect "yes): " { send "YES\n" }
expect ": "
sleep 1
send "passphrase\n"
expect ": "
sleep 1
send "passphrase\n"
expect eof
谢谢!
将超时值设置为-1或一个大数字
spawn cryptsetup luksFormat /dev/loop20
set timeout -1
expect "Are you sure? (Type uppercase yes): " { send "YES\n" }
expect "Enter passphrase for /dev/loop20: "
send "passphrase\n"
expect "Verify passphrase: "
send "passphrase\n"
expect eof