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

it_*_*ure 11 automation centos expect mariadb tcltk

环境: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!
Run Code Online (Sandbox Code Playgroud)

我编写了一个自动化期望脚本来安装 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}
  }
Run Code Online (Sandbox Code Playgroud)

要执行/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)
Run Code Online (Sandbox Code Playgroud)

如下写是没有用的:

  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}
  }
Run Code Online (Sandbox Code Playgroud)

同样的错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ler 5

这些脚本等待接收可选输出(timeout -1意味着“无超时”)并且它们可以区分不同的响应,因为它是yum install和 所要求的mysql_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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

出于调试目的 - 或为了验证答案,可以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]
Run Code Online (Sandbox Code Playgroud)

指令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
Run Code Online (Sandbox Code Playgroud)

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

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


最有信誉的来源,我可以提供是expect手动,通过多恩·利伯斯@ NIST和TCL / TK手册编写的expect,它的SourceForge项目不约而同地叫一起expect