skip to Main Content

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 "yr";exp_continue}
      "New password:" {send "123456r";exp_continue}
      "Re-enter new password:" {send "123456r";exp_continue}
      "Remove anonymous users? [Y/n]" {send "yr";exp_continue}
      "Disallow root login remotely? [Y/n]" {send "yr";exp_continue}
      "Remove test database and access to it? [Y/n]" {send "yr";exp_continue}
      "Reload privilege tables now? [Y/n]" {send "yr";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 "yr";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 "yr";exp_continue}
      "New password:" {send "123456r";exp_continue}
      "Re-enter new password:" {send "123456r";exp_continue}
      "Remove anonymous users? \[Y/n]" {send "yr";exp_continue}
      "Disallow root login remotely? \[Y/n]" {send "yr";exp_continue}
      "Remove test database and access to it? \[Y/n]" {send "yr";exp_continue}
      "Reload privilege tables now? \[Y/n]" {send "yr";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 "yr";exp_conti..."
    (file "secure.exp" line 3)

How to fix my exp script then?

2

Answers


  1. Not only are square brackets used for command substitution, but they are also special for glob patterns.

    You can either use the -exact switch while escaping the square brackets in quotes:

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

    Or use braces instead of quotes:

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

    FYI you can have the expect script generated for you by using autoexpect:

    autoexpect ./mysql_secure_installation
    

    This will generate an expect script called script.exp in your present working directory.

    Login or Signup to reply.
  2. These scripts wait to receive optional output (timeout -1 means “no timeout”) and they can tell apart different responses, as it is required by yum install and mysql_secure_installation. With #!/bin/expect -f as shebang, the scripts can be executed, when they were set to chmod +x.

    A) To begin with, mariadb_yum.exp (requires su or sudo):

    #!/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 "$PASSWORDr"
    }
    
    expect {
        # when the package was already installed
        -ex "Nothing to do" {
            send_user "package was already installedn"
        }
        # when the package had been installed
        -ex "Complete!" {
            send_user "package had been installedn"
        }
    }
    
    expect eof
    close $yum_spawn_id
    exit 0
    

    B) And then mariadb_sec.exp (doesn’t require 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 "$PASSWORDr"
    
    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 "nr"
        }
        # when there is no root password set (could not test this branch).
        -ex "Set root password? [Y/n] " {
            exp_send "Yr"
            expect -ex "New password: "
            exp_send "$PASSWORDr"
            expect -ex "Re-enter new password: "
            exp_send "$PASSWORDr"
        }
    }
    expect -ex "Remove anonymous users? [Y/n] "
    exp_send "Yr"
    expect -ex "Disallow root login remotely? [Y/n] "
    exp_send "Yr"
    expect -ex "Remove test database and access to it? [Y/n] "
    exp_send "Yr"
    expect -ex "Reload privilege tables now? [Y/n] "
    exp_send "Yr"
    
    expect eof
    close $mysql_spawn_id
    exit 0
    

    For debugging purposes – or to validate the answer, one can run expect with log-level strace 4. This is probably as reputable as a source can get, when it comes to writing expect scripts, as it nicely displays what is going on and most importantly, in which order the things happen:

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

    Instruction set exp_internal 1 can be used to get output for the regex-matching.


    A possible source of confusion might be, where one spawns the processes – as one can spawn several process on various hosts, eg. ssh locally and then yum and mysql_secure_installation remotely. Added $spawn_id to the script; the bottom one close call might be redundant, since it is already EOF (just to show how to spawn & close processes):

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

    Conclusion: The mariadb_sec.exp script probably could be improved further, eg. when at first sending no password and seeing what happens – then sending the password on ERROR 1045 (when a password had already been set previously). It may be save to assume, that one has to set the password when the server just has been installed (except that yum reinstall delivers the same result). Just had no blank CentOS container to test all the cases. Unless running in a root shell, passing both kinds of passwords into one script would be required to automate this from installation until post-installation.

    Probably worth noting is that on GCE, sudo would not ask for a password; there are indeed minor differences based upon the environment, as these CentOS container images behave differently. In such case (since there is no su or container-image detection in place), the mariadb_yum.exp script might get stuck for 30 seconds and then continue.


    The most reputable sources I can offer are the expect manual, written by Don Libes @ NIST and the TCL/TK manual for expect, along with it’s SourceForge project coincidentally called expect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search