如何编写安装mariadb的expect脚本?

How to write the expect script to install mariadb?

环境:centos7 + mariadb5.5.64.
让我在 运行 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!

我写了一个自动化期望脚本来安装 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}
  }

要执行 /usr/bin/expect secure.exp,我遇到错误:

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)

这样写是没有用的:

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

同样的错误:

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)

那么如何修复我的 exp 脚本?

不仅方括号用于命令替换,they are also special for glob patterns.

您可以在转义引号中的方括号时使用 -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:

为您生成 expect 脚本
autoexpect ./mysql_secure_installation

这将在您当前的工作目录中生成一个名为 script.exp 的预期脚本。

这些脚本等待接收可选输出(timeout -1 表示 "no timeout")并且它们可以区分不同的响应,因为它是 yum installmysql_secure_installation 所要求的。使用 #!/bin/expect -f 作为 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 日志级别 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,然后在远程 yummysql_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 容器来测试所有情况。除非 运行 在 root shell 中,将这两种密码传递到一个脚本中将需要从安装到 post-安装自动化。

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


我可以提供的最有信誉的来源是 expect manual, written by Don Libes @ NIST and the TCL/TK manual for expect, along with it's SourceForge project coincidentally called expect