【问题标题】:Best strategy for automatize a flow that need user interaction自动化需要用户交互的流程的最佳策略
【发布时间】:2016-01-29 14:03:24
【问题描述】:

我在 Dockerfile 中工作,但我需要运行一个“安全”的 MariaDB 服务器脚本,它是交互式的,我不知道如何处理。

基本上这是我在测试环境中在脚本上遵循的流程,与我想在 Dockerfile 中实现的流程相同,无需用户交互,只需按照您在下面的流程中看到的回答即可:

# /usr/bin/mysql_secure_installation

Enter current password for root (enter for none): [ENTER] // because there is no password
OK, successfully used password, moving on...

Set root password? [Y/n] n
 ... skipping.  

Remove anonymous users? [Y/n] Y
 ... Success!

Disallow root login remotely? [Y/n] n
 ... skipping.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

所以我需要编写一个 bash 脚本或其他可以自动处理此问题但不知道的东西,您将如何处理?

【问题讨论】:

    标签: linux bash shell docker


    【解决方案1】:

    我认为您可以尝试两种不同的方法:

    1. 使用 expect 脚本最终从 bash “评估”。手册页here

    或者:

    1. 在 bash 脚本中使用“here document”语法(不需要)

    ** EDIT - 选项 2 示例 **

    #!/bin/bash
    ...
    /usr/bin/mysql_secure_installation <<EOF
    
    n
    Y
    n
    Y
    Y
    EOF
    ...
    

    基本上,您将所有答案传递给 mysql_secure_installation 的标准输入。

    使用expect 只是稍微复杂一点。你可以从here开始

    【讨论】:

    • 太好了,我现在明白了,这只是另一个疑问:如果我把它放在一个名为 secure.sh 的脚本中,这个脚本会调用 /usr/bin/mysql_secure_installation 吗?我的意思是我仍然不明白here document 是如何工作的,因为在我看来我应该先调用secure_installation 脚本然后回答,你能清除这个吗?
    • 现在...是的。我之前的版本有错别字。
    【解决方案2】:

    另一个选项,mysql_secure_installation 是一个简单的 bash 脚本,它执行几个 mysql 命令,只需使用 sql 命令。

    DELETE FROM mysql.user WHERE User='';
    DROP DATABASE test;
    DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
    FLUSH PRIVILEGES;
    

    将此命令保存在文件中并将其通过管道传输到 mysql

    mysql < my_file.sql
    

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      相关资源
      最近更新 更多