bash - 使用 expect 确认密码

标签 bash expect

我想做的是编写一个 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/

相关文章:

linux - 如果文件中仅存在头行,awk 停止处理

database - mysqldump 将 CREATE 放在 mysqldump 的第一行

bash - 在 Bash 中每 6 分钟运行一次命令

linux - 期望脚本+如果没有出现如何忽略字符串

bash - "bad variable name"使用 "read var"

linux - awk 在其他路径上运行时不起作用

linux - TCL Expect 正在向标准输出注入(inject)额外的空字符

linux - Expect/TCL 简单过程/变量问题

c - 无论如何,我可以在不安装的情况下使用expect吗?

expect - Expect 脚本还值得学习吗?