我想做的是编写一个 expect 脚本来备份服务器上的所有数据库,然后创建一个包含 sql 的加密 zip 文件(我无法让我的 tar/openssl 命令在 expect 中正常运行)文件。
这只是一个学习 expect 的练习,不是真正的备份解决方案。
我显然在这里缺乏一些理解。我想做的是:
- 将所有数据库备份到一个文件(完成)
- 运行 zip 命令创建一个加密的 zip 文件(完成……有点)
- 回复“输入密码:”
然后回复确认“验证密码:”
#!/usr/bin/expect -f exp_internal 1 set backupdir "/mnt/db-backups/" set now [clock seconds] set date [clock format $now -format {%Y-%m-%d}] set filename $date append filename "_dbbackups.sql" exec mysqldump -u root --all-databases --events > $backupdir$filename spawn zip -e $backupdir$filename.enc.zip $backupdir$filename expect { "Enter password: " { send "monkey" exp_continue } "Verify password: " {send "monkey" exp_continue } }
输出是
$expect encrypt.sh
spawn zip -e /mnt/db-backups/2013-12-11_dbbackups.sql.enc.zip /mnt/db-backups/2013-12-11_dbbackups.sql
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {15733}
expect: does "" (spawn_id exp6) match glob pattern "Enter password: "? no
"Verify 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 "monkey" to { exp6 }
expect: continuing expect
expect: does "" (spawn_id exp6) match glob pattern "Enter password: "? no
"Verify password: "? no
monkey
expect: does "monkey" (spawn_id exp6) match glob pattern "Enter password: "? no
"Verify password: "? no
-- then I exited --
这是一个非常简单的脚本……但我很吃力。
最佳答案
问题很简单:
你必须按回车键。所以只需更改命令
send "monkey"
到
send "monkey\r"
(两者)
关于bash - 使用 expect 确认密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510301/