我正在尝试创建一个使用 kpcli 的 bash 脚本,以便自动将条目输入到 kdbx 文件中。在这里搜索时,我发现您可以使用 expect 和 send,但这似乎对我不起作用。
set timeout 10
spawn kpcli
match_max 100000000
expect "kpcli:/>"
send "open global.kdbx\n"
expect "Please provide the master password:"
send "mypassword"
expect "kpcli:/>"
send "cd Websites/"
while IFS=" " read -r domainname username password
do
expect "kpcli:/Websites>"
send "new "$domainname""
expect "Username:"
send ""$username""
expect "Password:"
send ""$password""
expect "Retype to verify: "
send "$password"
expect "URL:"
send ""$domainname""
expect "Tags:"
send "\n"
expect "Strings: (a)dd/(e)dit/(d)elete/(c)ancel/(F)inish?"
send "F"
send "\n"
expect "Database was modified. Do you want to save it now? [y/N]: "
send "y"
send "y"
done < sites.txt
这是可行的方法吗?或者有更好的方法吗?
答案1
问题是你混合使用了 expect 和 bash 语法。可以混合使用两者,但只选择一种更简洁。以下是你在 expect 中的代码:
#!/usr/bin/expect
set timeout 10
match_max 100000000
spawn kpcli
expect "kpcli:/>"
send "open global.kdbx\r"
expect "Please provide the master password:"
send -- "mypassword\r"
expect "kpcli:/>"
send "cd Websites/\r"
set filehandle [open sites.txt r]
while {[gets $filehandle line] != -1} {
lassign [split $line] domainname username password
expect "kpcli:/Websites>"
send "new $domainname\r"
expect "Username:"
send -- "$username\r"
expect "Password:"
send -- "$password\r"
expect "Retype to verify: "
send -- "$password\r"
expect "URL:"
send -- "$domainname\r"
expect "Tags:"
send "\r"
expect "Strings: (a)dd/(e)dit/(d)elete/(c)ancel/(F)inish?"
send "F\r"
expect "Database was modified. Do you want to save it now? [y/N]: "
send "y\r"
}
close $filehandle
我对你必须按 Enter 键的频率做了一些假设(\r
)