11

Environment: centos7 + mariadb5.5.64.
Let me show the installation info on screen when to run mysql_secure_installation.

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

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

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

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...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

I write an automation expect script to install mariadb.

  vim secure.exp
  set timeout 60
  spawn mysql_secure_installation
  expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? [Y/n] " {send "y\r";exp_continue}
      "New password:" {send "123456\r";exp_continue}
      "Re-enter new password:" {send "123456\r";exp_continue}
      "Remove anonymous users? [Y/n]" {send "y\r";exp_continue}
      "Disallow root login remotely? [Y/n]" {send "y\r";exp_continue}
      "Remove test database and access to it? [Y/n]" {send "y\r";exp_continue}
      "Reload privilege tables now? [Y/n]" {send "y\r";exp_continue}
  }

To execute /usr/bin/expect secure.exp, i come across the error:

spawn mysql_secure_installation
invalid command name "Y/n"
    while executing
"Y/n"
    invoked from within
"expect {
          "Enter current password for root (enter for none): " {send "\r";exp_continue}
          "Set root password? [Y/n] " {send "y\r";exp..."
    (file "secure.exp" line 3)

It is no use to write as below:

  set timeout 60
  spawn mysql_secure_installation
  expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? \\[Y/n] " {send "y\r";exp_continue}
      "New password:" {send "123456\r";exp_continue}
      "Re-enter new password:" {send "123456\r";exp_continue}
      "Remove anonymous users? \\[Y/n]" {send "y\r";exp_continue}
      "Disallow root login remotely? \\[Y/n]" {send "y\r";exp_continue}
      "Remove test database and access to it? \\[Y/n]" {send "y\r";exp_continue}
      "Reload privilege tables now? \\[Y/n]" {send "y\r";exp_continue}
  }

Same error:

invalid command name "Y/n"
    while executing
"Y/n"
    invoked from within
"expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? \\[Y/n] " {send "y\r";exp_conti..."
    (file "secure.exp" line 3)

How to fix my exp script then?

4

2 回答 2

5

这些脚本等待接收可选输出(timeout -1意味着“无超时”),它们可以区分不同的响应,因为它是 and 所要求yum installmysql_secure_installation。使用#!/bin/expect -fas shebang,脚本可以在设置为 时执行chmod +x

A) 首先,mariadb_yum.exp(需要susudo):

#!/bin/expect -f
set timeout 30
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_yum.exp \[linux sudo password\]\n"
    exit 1
}
set USERNAME "[exec whoami]"
set PASSWORD [lindex $argv 0];

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/$USERNAME/mariadb_install.log"

spawn sudo yum -y install MariaDB-server
set yum_spawn_id $spawn_id

# On GCE it will never ask for a sudo password:
expect -ex "\[sudo\] password for $USERNAME: " {
   exp_send "$PASSWORD\r"
}

expect {
    # when the package was already installed
    -ex "Nothing to do" {
        send_user "package was already installed\n"
    }
    # when the package had been installed
    -ex "Complete!" {
        send_user "package had been installed\n"
    }
}

expect eof
close $yum_spawn_id
exit 0

B)然后mariadb_sec.exp(不需要sudo):

#!/bin/expect -f
set timeout 1
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_sec.exp \[mysql root password\]\n"
    exit 1
}
set PASSWORD [lindex $argv 0];

spawn mysql_secure_installation
set mysql_spawn_id $spawn_id

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/[exec whoami]/mariadb_install.log"

# when there is no password set, this probably should be "\r"
expect -ex "Enter current password for root (enter for none): "
exp_send "$PASSWORD\r"

expect {
    # await an eventual error message
    -ex "ERROR 1045" {
        send_user "\nMariaDB > An invalid root password had been provided.\n"
        close $mysql_spawn_id
        exit 1
    }
    # when there is a root password set
    -ex "Change the root password? \[Y/n\] " {
        exp_send "n\r"
    }
    # when there is no root password set (could not test this branch).
    -ex "Set root password? \[Y/n\] " {
        exp_send "Y\r"
        expect -ex "New password: "
        exp_send "$PASSWORD\r"
        expect -ex "Re-enter new password: "
        exp_send "$PASSWORD\r"
    }
}
expect -ex "Remove anonymous users? \[Y/n\] "
exp_send "Y\r"
expect -ex "Disallow root login remotely? \[Y/n\] "
exp_send "Y\r"
expect -ex "Remove test database and access to it? \[Y/n\] "
exp_send "Y\r"
expect -ex "Reload privilege tables now? \[Y/n\] "
exp_send "Y\r"

expect eof
close $mysql_spawn_id
exit 0

出于调试目的 - 或验证答案,可以expect使用 log-level运行strace 4。在编写脚本时,这可能与来源所能获得的声誉一样高expect,因为它很好地显示了正在发生的事情,最重要的是,事情发生的顺序:

expect -c "strace 4" ./mariadb_yum.exp [linux sudo password]
expect -c "strace 4" ./mariadb_sec.exp [mysql root password]

指令set exp_internal 1可用于获取正则表达式匹配的输出。


一个可能的混淆来源可能是一个产生进程的地方 - 因为一个人可以在不同的主机上产生多个进程,例如。ssh本地然后yum远程mysql_secure_installation。添加$spawn_id到脚本中;最下面的一个close调用可能是多余的,因为它已经是EOF(只是为了展示如何处理spawnclose

Thanks for using MariaDB!
 1  close $mysql_spawn_id
 1  exit 0
 2  rename _close.pre_expect close

结论:mariadb_sec.exp脚本可能可以进一步改进,例如。起初不发送密码并查看会发生什么时 - 然后发送密码ERROR 1045(之前已经设置了密码)。可以假设,当服务器刚刚安装时必须设置密码(除了yum reinstall提供相同的结果)。只是没有空白的 CentOS 容器来测试所有案例。除非在rootshell 中运行,否则需要将两种密码都传递到一个脚本中,以便从安装到安装后自动执行此操作。

可能值得注意的是,在 GCE 上,sudo不会要求输入密码;确实存在基于环境的细微差别,因为这些 CentOS 容器映像的行为不同。在这种情况下(因为没有su或容器图像检测到位),mariadb_yum.exp脚本可能会卡住30几秒钟,然后继续。


我能提供的最有信誉的来源是expect由 Don Libes @ NIST 编写的手册和 TCL/TK 手册expect,以及巧合称为 的 SourceForge 项目expect

于 2020-02-09T21:24:49.943 回答
2

方括号不仅用于命令替换,而且对于 glob 模式也是特殊的

您可以-exact在转义引号中的方括号时使用开关:

spawn mysql_secure_installation
expect {
    ...
    -exact "Set root password? \[Y/n\] " {send "y\r";exp_continue}
    ...
}

或者使用大括号代替引号:

spawn mysql_secure_installation
expect {
    ...
    {Set root password? \[Y/n\] } {send "y\r";exp_continue}
    ...
}

仅供参考,您可以使用以下命令为您生成期望脚本autoexpect

autoexpect ./mysql_secure_installation

script.exp这将生成一个在您当前工作目录中调用的期望脚本。

于 2020-02-09T20:48:28.870 回答