मरदब को स्थापित करने के लिए उम्मीद की पटकथा कैसे लिखनी है?


11

पर्यावरण: सेंटोस 7 + मारीदब 5.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)

फिर मेरी ऍक्स्प स्क्रिप्ट को कैसे ठीक करें?


उम्मीद एक Tcl एक्सटेंशन है। Tcl में, [ ... ]है आदेश प्रतिस्थापन जो खोल की तरह है $( ... )। इसलिए के "Set root password? [Y/n] "रूप में लिखा जाना चाहिए "Set root password? \\[Y/n] "
pynexj

जवाबों:


5

ये स्क्रिप्ट वैकल्पिक आउटपुट प्राप्त करने के लिए प्रतीक्षा करती हैं ( timeout -1जिसका अर्थ है "नो टाइमआउट") और वे अलग-अलग प्रतिक्रियाओं को बता सकते हैं, जैसा कि इसके द्वारा आवश्यक है yum installऔर mysql_secure_installation#!/bin/expect -fशेबबैंग के साथ , स्क्रिप्ट को निष्पादित किया जा सकता है, जब उन्हें सेट किया गया था chmod +x

ए) के साथ शुरू करने के लिए, mariadb_yum.exp(की आवश्यकता है suया 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

बी) और फिर 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स्थानीय और फिर yumऔर mysql_secure_installationदूर से। $spawn_idस्क्रिप्ट में जोड़ा गया ; नीचे एक closeकॉल निरर्थक हो सकती है, क्योंकि यह पहले से ही है EOF(सिर्फ यह दिखाने के लिए कि कैसे spawnऔर closeप्रक्रियाएं):

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खोल में नहीं चल रहा है , दोनों प्रकार के पासवर्ड को एक स्क्रिप्ट में पारित करने के लिए स्थापना से इसे स्वचालित करने के लिए आवश्यक होगा, स्थापना के बाद।

संभवतः ध्यान देने योग्य बात यह है कि जीसीई पर, sudoपासवर्ड नहीं मांगेगा; पर्यावरण पर आधारित वास्तव में मामूली अंतर हैं, क्योंकि ये सेंटोस कंटेनर छवियां अलग तरह से व्यवहार करती हैं। ऐसे मामले में (क्योंकि suजगह में कंटेनर या इमेज का पता नहीं है), mariadb_yum.expस्क्रिप्ट 30सेकंड के लिए अटक सकती है और फिर जारी रह सकती है।


सबसे प्रतिष्ठित स्रोत जो मैं पेश कर सकता हूं expect, वह मैनुअल है, जिसे डॉन लिबस @ एनआईएसटी और टीसीएल / टीके मैनुअल द्वारा लिखा गया है expect, इसके साथ-साथ इसे सोर्सफॉर्ज प्रोजेक्ट भी कहा जाता है expect


2

न केवल वर्ग ब्रैकेट कमांड प्रतिस्थापन के लिए उपयोग किए जाते हैं , बल्कि वे ग्लोब पैटर्न के लिए भी विशेष हैं

आप या तो -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}
    ...
}

FYI करें आप उम्मीद कर सकते हैं कि स्क्रिप्ट आपके द्वारा उत्पन्न की गई है autoexpect:

autoexpect ./mysql_secure_installation

यह script.expआपके वर्तमान कार्यशील निर्देशिका में अपेक्षित स्क्रिप्ट उत्पन्न करेगा ।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.