Bagaimana cara menulis skrip harapan untuk menginstal mariadb?

11

Lingkungan: centos7 + mariadb5.5.64.
Biarkan saya menunjukkan info instalasi di layar kapan harus dijalankan 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!

Saya menulis skrip mengharapkan otomatisasi untuk menginstal 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}
  }

Untuk mengeksekusi /usr/bin/expect secure.exp, saya menemukan kesalahan:

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)

Tidak ada gunanya menulis seperti di bawah ini:

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

Kesalahan yang sama:

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)

Bagaimana cara memperbaiki skrip exp saya?

it_is_a_literature
sumber
Harapkan adalah ekstensi Tcl. Di Tcl, [ ... ]adalah substitusi perintah yang seperti shell $( ... ). Jadi "Set root password? [Y/n] "harus ditulis sebagai "Set root password? \\[Y/n] ".
pynexj

Jawaban:

5

Script ini menunggu untuk menerima output opsional ( timeout -1berarti "tidak ada batas waktu") dan mereka dapat membedakan respon yang berbeda, karena diperlukan oleh yum installdan mysql_secure_installation. Dengan #!/bin/expect -fsebagai shebang, skrip dapat dieksekusi, ketika diatur chmod +x.

A) Untuk memulainya, mariadb_yum.exp(membutuhkan suatau 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 "$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) Dan kemudian mariadb_sec.exp(tidak perlu 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

Untuk keperluan debugging - atau untuk memvalidasi jawaban, seseorang dapat menjalankan expectdengan tingkat log strace 4. Ini mungkin memiliki reputasi sebagai sumber yang bisa didapat, ketika datang untuk menulis expectskrip, karena baik menampilkan apa yang sedang terjadi dan yang paling penting, di mana urutan hal-hal terjadi:

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

Instruksi set exp_internal 1dapat digunakan untuk mendapatkan output untuk pencocokan regex.


Sumber kebingungan yang mungkin adalah, di mana seseorang memunculkan proses - karena orang dapat menelurkan beberapa proses pada berbagai host, misalnya. sshlokal dan kemudian yumdan mysql_secure_installationjarak jauh. Ditambahkan$spawn_id ke skrip; closepanggilan satu bawah mungkin berlebihan, karena sudah EOF(hanya untuk menunjukkan bagaimana caranya spawn& closeproses):

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

Kesimpulan: mariadb_sec.expSkrip mungkin dapat ditingkatkan lebih lanjut, misalnya. ketika pada awalnya mengirim kata sandi dan melihat apa yang terjadi - kemudian mengirim kata sandi aktif ERROR 1045(ketika kata sandi telah ditetapkan sebelumnya). Mungkin aman untuk berasumsi, bahwa seseorang harus mengatur kata sandi ketika server baru saja diinstal (kecuali yang yum reinstallmemberikan hasil yang sama). Hanya saja tidak ada wadah CentOS kosong untuk menguji semua kasus. Kecuali jika dijalankan dalam rootshell, memasukkan kedua jenis kata sandi ke dalam satu skrip akan diperlukan untuk mengotomatisasi ini dari instalasi hingga pasca instalasi.

Mungkin perlu dicatat adalah bahwa pada GCE, sudo tidak akan meminta kata sandi; memang ada perbedaan kecil berdasarkan lingkungan, karena gambar wadah CentOS ini berperilaku berbeda. Dalam kasus seperti itu (karena tidak ada suatau deteksi gambar kontainer), mariadb_yum.expskrip mungkin macet selama beberapa 30detik dan kemudian melanjutkan.


Sumber yang paling terkemuka yang dapat saya tawarkan adalah expectmanual, ditulis oleh Don Libes @ NIST dan manual TCL / TK expect, bersama dengan proyek SourceForge yang kebetulan dipanggil expect.

Martin Zeitler
sumber
2

Tidak hanya tanda kurung siku yang digunakan untuk substitusi perintah, tetapi mereka juga khusus untuk pola glob .

Anda bisa menggunakan -exactsakelar saat keluar dari tanda kurung dengan tanda kutip:

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

Atau gunakan kawat gigi sebagai ganti tanda kutip:

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

FYI Anda dapat membuat skrip harapan dibuat untuk Anda dengan menggunakan autoexpect:

autoexpect ./mysql_secure_installation

Ini akan menghasilkan skrip harapan yang dipanggil script.expdi direktori kerja Anda saat ini.

mnistic
sumber