1 votes

Automatisez l'installation sécurisée de mysql en utilisant un script shell

Je suis en train d'essayer d'automatiser l'installation sécurisée de mysql en utilisant un script shell linux. J'ai le code ci-dessous obtenu à partir de https://gist.github.com/Mins/4602864.

#!/bin/bash

  MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
  MYSQL_ROOT_PASSWORD="test@123"

  SECURE_MYSQL=$(expect -c "

  set timeout 10
  spawn mysql_secure_installation

  expect "Enter password for user root:"
  send "$MYSQL\r"

  expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect "New password:"
  send "$MYSQL_ROOT_PASSWORD\r"

  expect "Re-enter new password:"
  send "$MYSQL_ROOT_PASSWORD\r"

  expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
  send "y\r"

  expect eof
  "))

  echo "$SECURE_MYSQL"

Mais je reçois une erreur

./sql.sh: command substitution: line 48: syntax error near unexpected token `('
./sql.sh: command substitution: line 48: `      expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'

J'ai essayé de comprendre l'erreur mais sans succès.

1voto

Zian Choy Points 1510

Essayez ceci tel quel, il semble qu'il y ait un problème avec ( et le pipe | donc j'ai dû les échapper.

Code source

#!/bin/bash

      MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
      MYSQL_ROOT_PASSWORD="test@123"

      SECURE_MYSQL=$(expect -c "

      set timeout 10
      spawn mysql_secure_installation

      expect "Enter password for user root:"
      send "$MYSQL\r"

      expect "Change the password for root ?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect "New password:"
      send "$MYSQL_ROOT_PASSWORD\r"

      expect "Re-enter new password:"
      send "$MYSQL_ROOT_PASSWORD\r"

      expect "Do you wish to continue with the password provided?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect "Remove anonymous users?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect "Disallow root login remotely?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect "Remove test database and access to it?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect "Reload privilege tables now?\(Press y\|Y for Yes, any other key for No\) :"
      send "y\r"

      expect eof
      ")

      echo "$SECURE_MYSQL"

Exécution de débogage

root@5015a2757ac4:/# bash -x secure.sh
++ grep 'temporary password' /var/log/mysqld.log
++ awk '{print $11}'
grep: /var/log/mysqld.log: Aucun fichier ou dossier de ce type
+ MYSQL=
+ MYSQL_ROOT_PASSWORD=test@123
++ expect -c '

      set timeout 10
      spawn mysql_secure_installation

      expect Enter' password for user 'root:
      send r

      expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect New' 'password:
      send test@123r

      expect Re-enter' new 'password:
      send test@123r

      expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
      send yr

      expect eof
      '

couldn't read file "password": no such file or directory
+ SECURE_MYSQL='spawn mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: '
+ echo 'spawn mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: '
spawn mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:
root@5015a2757ac4:/#

Et j'ai vérifié la connexion avec le nouveau mot de passe test@123.

0voto

Janith Points 339

Le script ci-dessous a fonctionné pour moi.

#!/bin/bash

  MYSQL_ROOT_PASSWORD='Password@123'
  MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

  SECURE_MYSQL=$(expect -c "

  set timeout 10
  spawn mysql_secure_installation

  expect \"Enter password for user root:\"
  send \"$MYSQL\r\"
  expect \"New password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Re-enter new password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Change the password for root ?\ ((Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  send \"$MYSQL\r\"
  expect \"New password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Re-enter new password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Do you wish to continue with the password provided?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Remove anonymous users?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Disallow root login remotely?\(Press y\|Y for Yes, any other key for No) :\"
  send \"n\r\"
  expect \"Remove test database and access to it?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Reload privilege tables now?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect eof
  ")

  echo $SECURE_MYSQL

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X